diff options
-rw-r--r-- | Android.mk | 14 | ||||
-rw-r--r-- | bootloader.cpp | 134 | ||||
-rw-r--r-- | bootloader.h | 157 | ||||
-rw-r--r-- | bootloader_message/Android.mk | 25 | ||||
-rw-r--r-- | bootloader_message/bootloader_message.cpp (renamed from uncrypt/bootloader_message_writer.cpp) | 82 | ||||
-rw-r--r-- | bootloader_message/include/bootloader_message/bootloader_message.h | 199 | ||||
-rw-r--r-- | device.cpp | 32 | ||||
-rw-r--r-- | error_code.h | 5 | ||||
-rw-r--r-- | install.cpp | 269 | ||||
-rw-r--r-- | install.h | 17 | ||||
-rw-r--r-- | minui/font_10x18.h | 8 | ||||
-rw-r--r-- | minui/graphics.cpp | 114 | ||||
-rw-r--r-- | minui/minui.h | 16 | ||||
-rw-r--r-- | minzip/SysUtil.cpp | 12 | ||||
-rw-r--r-- | recovery.cpp | 185 | ||||
-rw-r--r-- | screen_ui.cpp | 98 | ||||
-rw-r--r-- | screen_ui.h | 49 | ||||
-rw-r--r-- | uncrypt/Android.mk | 13 | ||||
-rw-r--r-- | uncrypt/include/bootloader_message_writer.h | 35 | ||||
-rw-r--r-- | uncrypt/uncrypt.cpp | 56 | ||||
-rw-r--r-- | update_verifier/Android.mk | 5 | ||||
-rw-r--r-- | update_verifier/update_verifier.cpp | 131 | ||||
-rw-r--r-- | wear_ui.cpp | 357 | ||||
-rw-r--r-- | wear_ui.h | 74 |
24 files changed, 1128 insertions, 959 deletions
diff --git a/Android.mk b/Android.mk index e502d9e6b..363f2df10 100644 --- a/Android.mk +++ b/Android.mk @@ -41,7 +41,6 @@ include $(CLEAR_VARS) LOCAL_SRC_FILES := \ adb_install.cpp \ asn1_decoder.cpp \ - bootloader.cpp \ device.cpp \ fuse_sdcard_provider.cpp \ install.cpp \ @@ -75,6 +74,7 @@ LOCAL_C_INCLUDES += \ LOCAL_STATIC_LIBRARIES := \ libbatterymonitor \ + libbootloader_message \ libext4_utils_static \ libsparse_static \ libminzip \ @@ -149,14 +149,16 @@ LOCAL_STATIC_LIBRARIES := libcrypto_utils libcrypto libbase LOCAL_CFLAGS := -Werror include $(BUILD_STATIC_LIBRARY) -include $(LOCAL_PATH)/minui/Android.mk \ - $(LOCAL_PATH)/minzip/Android.mk \ +include \ + $(LOCAL_PATH)/applypatch/Android.mk \ + $(LOCAL_PATH)/bootloader_message/Android.mk \ + $(LOCAL_PATH)/edify/Android.mk \ $(LOCAL_PATH)/minadbd/Android.mk \ + $(LOCAL_PATH)/minui/Android.mk \ + $(LOCAL_PATH)/minzip/Android.mk \ + $(LOCAL_PATH)/otafault/Android.mk \ $(LOCAL_PATH)/tests/Android.mk \ $(LOCAL_PATH)/tools/Android.mk \ - $(LOCAL_PATH)/edify/Android.mk \ $(LOCAL_PATH)/uncrypt/Android.mk \ - $(LOCAL_PATH)/otafault/Android.mk \ $(LOCAL_PATH)/updater/Android.mk \ $(LOCAL_PATH)/update_verifier/Android.mk \ - $(LOCAL_PATH)/applypatch/Android.mk diff --git a/bootloader.cpp b/bootloader.cpp deleted file mode 100644 index dad0bab30..000000000 --- a/bootloader.cpp +++ /dev/null @@ -1,134 +0,0 @@ -/* - * Copyright (C) 2008 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include <errno.h> -#include <fcntl.h> -#include <inttypes.h> -#include <stdio.h> -#include <string.h> -#include <sys/stat.h> -#include <sys/types.h> -#include <unistd.h> - -#include <fs_mgr.h> - -#include <android-base/logging.h> -#include <android-base/unique_fd.h> - -#include "bootloader.h" -#include "roots.h" - -static int get_bootloader_message_block(bootloader_message* out, const Volume* v); -static int set_bootloader_message_block(const bootloader_message* in, const Volume* v); - -int get_bootloader_message(bootloader_message* out) { - Volume* v = volume_for_path("/misc"); - if (v == nullptr) { - LOG(ERROR) << "Cannot load volume /misc!"; - return -1; - } - if (strcmp(v->fs_type, "emmc") == 0) { - return get_bootloader_message_block(out, v); - } - LOG(ERROR) << "unknown misc partition fs_type \"" << v->fs_type << "\""; - return -1; -} - -int set_bootloader_message(const bootloader_message* in) { - Volume* v = volume_for_path("/misc"); - if (v == nullptr) { - LOG(ERROR) << "Cannot load volume /misc!"; - return -1; - } - if (strcmp(v->fs_type, "emmc") == 0) { - return set_bootloader_message_block(in, v); - } - LOG(ERROR) << "unknown misc partition fs_type \"" << v->fs_type << "\""; - return -1; -} - -// ------------------------------------ -// for misc partitions on block devices -// ------------------------------------ - -static void wait_for_device(const char* fn) { - int tries = 0; - int ret; - do { - ++tries; - struct stat buf; - ret = stat(fn, &buf); - if (ret == -1) { - printf("failed to stat \"%s\" try %d: %s\n", fn, tries, strerror(errno)); - sleep(1); - } - } while (ret && tries < 10); - - if (ret) { - printf("failed to stat \"%s\"\n", fn); - } -} - -static int get_bootloader_message_block(bootloader_message* out, - const Volume* v) { - wait_for_device(v->blk_device); - FILE* f = fopen(v->blk_device, "rb"); - if (f == nullptr) { - PLOG(ERROR) << "failed to open \"" << v->blk_device << "\""; - return -1; - } - bootloader_message temp; - int count = fread(&temp, sizeof(temp), 1, f); - if (count != 1) { - PLOG(ERROR) << "failed to read \"" << v->blk_device << "\""; - return -1; - } - if (fclose(f) != 0) { - PLOG(ERROR) << "failed to close \"" << v->blk_device << "\""; - return -1; - } - memcpy(out, &temp, sizeof(temp)); - return 0; -} - -static int set_bootloader_message_block(const bootloader_message* in, - const Volume* v) { - wait_for_device(v->blk_device); - android::base::unique_fd fd(open(v->blk_device, O_WRONLY | O_SYNC)); - if (fd == -1) { - PLOG(ERROR) << "failed to open \"" << v->blk_device << "\""; - return -1; - } - - size_t written = 0; - const uint8_t* start = reinterpret_cast<const uint8_t*>(in); - size_t total = sizeof(*in); - while (written < total) { - ssize_t wrote = TEMP_FAILURE_RETRY(write(fd, start + written, total - written)); - if (wrote == -1) { - PLOG(ERROR) << "failed to write " << total << " bytes, " << written - << " bytes written"; - return -1; - } - written += wrote; - } - - if (fsync(fd) == -1) { - PLOG(ERROR) << "failed to fsync \"" << v->blk_device << "\""; - return -1; - } - return 0; -} diff --git a/bootloader.h b/bootloader.h index 1801705fb..9c84a1cf9 100644 --- a/bootloader.h +++ b/bootloader.h @@ -14,158 +14,5 @@ * limitations under the License. */ -#ifndef _RECOVERY_BOOTLOADER_H -#define _RECOVERY_BOOTLOADER_H - -#include <assert.h> - -/* Bootloader Message (2-KiB) - * - * This structure describes the content of a block in flash - * that is used for recovery and the bootloader to talk to - * each other. - * - * The command field is updated by linux when it wants to - * reboot into recovery or to update radio or bootloader firmware. - * It is also updated by the bootloader when firmware update - * is complete (to boot into recovery for any final cleanup) - * - * The status field is written by the bootloader after the - * completion of an "update-radio" or "update-hboot" command. - * - * The recovery field is only written by linux and used - * for the system to send a message to recovery or the - * other way around. - * - * The stage field is written by packages which restart themselves - * multiple times, so that the UI can reflect which invocation of the - * package it is. If the value is of the format "#/#" (eg, "1/3"), - * the UI will add a simple indicator of that status. - * - * We used to have slot_suffix field for A/B boot control metadata in - * this struct, which gets unintentionally cleared by recovery or - * uncrypt. Move it into struct bootloader_message_ab to avoid the - * issue. - */ -struct bootloader_message { - char command[32]; - char status[32]; - char recovery[768]; - - // The 'recovery' field used to be 1024 bytes. It has only ever - // been used to store the recovery command line, so 768 bytes - // should be plenty. We carve off the last 256 bytes to store the - // stage string (for multistage packages) and possible future - // expansion. - char stage[32]; - - // The 'reserved' field used to be 224 bytes when it was initially - // carved off from the 1024-byte recovery field. Bump it up to - // 1184-byte so that the entire bootloader_message struct rounds up - // to 2048-byte. - char reserved[1184]; -}; - -/** - * We must be cautious when changing the bootloader_message struct size, - * because A/B-specific fields may end up with different offsets. - */ -#if (__STDC_VERSION__ >= 201112L) || defined(__cplusplus) -static_assert(sizeof(struct bootloader_message) == 2048, - "struct bootloader_message size changes, which may break A/B devices"); -#endif - -/** - * The A/B-specific bootloader message structure (4-KiB). - * - * We separate A/B boot control metadata from the regular bootloader - * message struct and keep it here. Everything that's A/B-specific - * stays after struct bootloader_message, which should be managed by - * the A/B-bootloader or boot control HAL. - * - * The slot_suffix field is used for A/B implementations where the - * bootloader does not set the androidboot.ro.boot.slot_suffix kernel - * commandline parameter. This is used by fs_mgr to mount /system and - * other partitions with the slotselect flag set in fstab. A/B - * implementations are free to use all 32 bytes and may store private - * data past the first NUL-byte in this field. It is encouraged, but - * not mandatory, to use 'struct bootloader_control' described below. - */ -struct bootloader_message_ab { - struct bootloader_message message; - char slot_suffix[32]; - - // Round up the entire struct to 4096-byte. - char reserved[2016]; -}; - -/** - * Be cautious about the struct size change, in case we put anything post - * bootloader_message_ab struct (b/29159185). - */ -#if (__STDC_VERSION__ >= 201112L) || defined(__cplusplus) -static_assert(sizeof(struct bootloader_message_ab) == 4096, - "struct bootloader_message_ab size changes"); -#endif - -#define BOOT_CTRL_MAGIC 0x42414342 /* Bootloader Control AB */ -#define BOOT_CTRL_VERSION 1 - -struct slot_metadata { - // Slot priority with 15 meaning highest priority, 1 lowest - // priority and 0 the slot is unbootable. - uint8_t priority : 4; - // Number of times left attempting to boot this slot. - uint8_t tries_remaining : 3; - // 1 if this slot has booted successfully, 0 otherwise. - uint8_t successful_boot : 1; - // 1 if this slot is corrupted from a dm-verity corruption, 0 - // otherwise. - uint8_t verity_corrupted : 1; - // Reserved for further use. - uint8_t reserved : 7; -} __attribute__((packed)); - -/* Bootloader Control AB - * - * This struct can be used to manage A/B metadata. It is designed to - * be put in the 'slot_suffix' field of the 'bootloader_message' - * structure described above. It is encouraged to use the - * 'bootloader_control' structure to store the A/B metadata, but not - * mandatory. - */ -struct bootloader_control { - // NUL terminated active slot suffix. - char slot_suffix[4]; - // Bootloader Control AB magic number (see BOOT_CTRL_MAGIC). - uint32_t magic; - // Version of struct being used (see BOOT_CTRL_VERSION). - uint8_t version; - // Number of slots being managed. - uint8_t nb_slot : 3; - // Number of times left attempting to boot recovery. - uint8_t recovery_tries_remaining : 3; - // Ensure 4-bytes alignment for slot_info field. - uint8_t reserved0[2]; - // Per-slot information. Up to 4 slots. - struct slot_metadata slot_info[4]; - // Reserved for further use. - uint8_t reserved1[8]; - // CRC32 of all 28 bytes preceding this field (little endian - // format). - uint32_t crc32_le; -} __attribute__((packed)); - -#if (__STDC_VERSION__ >= 201112L) || defined(__cplusplus) -static_assert(sizeof(struct bootloader_control) == - sizeof(((struct bootloader_message_ab *)0)->slot_suffix), - "struct bootloader_control has wrong size"); -#endif - -/* Read and write the bootloader command from the "misc" partition. - * These return zero on success. - */ -int get_bootloader_message(struct bootloader_message *out); -int set_bootloader_message(const struct bootloader_message *in); - -#endif +// TODO: Remove this file once we remove all places that include this file. +#include "bootloader_message/include/bootloader_message/bootloader_message.h" diff --git a/bootloader_message/Android.mk b/bootloader_message/Android.mk new file mode 100644 index 000000000..a8c50819b --- /dev/null +++ b/bootloader_message/Android.mk @@ -0,0 +1,25 @@ +# Copyright (C) 2016 The Android Open Source Project +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +LOCAL_PATH := $(call my-dir) + +include $(CLEAR_VARS) +LOCAL_CLANG := true +LOCAL_SRC_FILES := bootloader_message.cpp +LOCAL_MODULE := libbootloader_message +LOCAL_STATIC_LIBRARIES := libbase libfs_mgr +LOCAL_CFLAGS := -Werror +LOCAL_C_INCLUDES := $(LOCAL_PATH)/include +LOCAL_EXPORT_C_INCLUDE_DIRS := $(LOCAL_PATH)/include +include $(BUILD_STATIC_LIBRARY) diff --git a/uncrypt/bootloader_message_writer.cpp b/bootloader_message/bootloader_message.cpp index db52121eb..e0c95d223 100644 --- a/uncrypt/bootloader_message_writer.cpp +++ b/bootloader_message/bootloader_message.cpp @@ -14,6 +14,8 @@ * limitations under the License. */ +#include <bootloader_message/bootloader_message.h> + #include <errno.h> #include <fcntl.h> #include <string.h> @@ -27,8 +29,6 @@ #include <android-base/unique_fd.h> #include <fs_mgr.h> -#include "bootloader.h" - static struct fstab* read_fstab(std::string* err) { std::string ro_hardware = android::base::GetProperty("ro.hardware", ""); if (ro_hardware.empty()) { @@ -57,7 +57,57 @@ static std::string get_misc_blk_device(std::string* err) { return record->blk_device; } -static bool write_bootloader_message(const bootloader_message& boot, std::string* err) { +// In recovery mode, recovery can get started and try to access the misc +// device before the kernel has actually created it. +static bool wait_for_device(const std::string& blk_device, std::string* err) { + int tries = 0; + int ret; + err->clear(); + do { + ++tries; + struct stat buf; + ret = stat(blk_device.c_str(), &buf); + if (ret == -1) { + *err += android::base::StringPrintf("failed to stat %s try %d: %s\n", + blk_device.c_str(), tries, strerror(errno)); + sleep(1); + } + } while (ret && tries < 10); + + if (ret) { + *err += android::base::StringPrintf("failed to stat %s\n", blk_device.c_str()); + } + return ret == 0; +} + +static bool read_misc_partition(void* p, size_t size, size_t offset, std::string* err) { + std::string misc_blk_device = get_misc_blk_device(err); + if (misc_blk_device.empty()) { + return false; + } + if (!wait_for_device(misc_blk_device, err)) { + return false; + } + android::base::unique_fd fd(open(misc_blk_device.c_str(), O_RDONLY)); + if (fd.get() == -1) { + *err = android::base::StringPrintf("failed to open %s: %s", misc_blk_device.c_str(), + strerror(errno)); + return false; + } + if (lseek(fd.get(), static_cast<off_t>(offset), SEEK_SET) != static_cast<off_t>(offset)) { + *err = android::base::StringPrintf("failed to lseek %s: %s", misc_blk_device.c_str(), + strerror(errno)); + return false; + } + if (!android::base::ReadFully(fd.get(), p, size)) { + *err = android::base::StringPrintf("failed to read %s: %s", misc_blk_device.c_str(), + strerror(errno)); + return false; + } + return true; +} + +static bool write_misc_partition(const void* p, size_t size, size_t offset, std::string* err) { std::string misc_blk_device = get_misc_blk_device(err); if (misc_blk_device.empty()) { return false; @@ -68,11 +118,17 @@ static bool write_bootloader_message(const bootloader_message& boot, std::string strerror(errno)); return false; } - if (!android::base::WriteFully(fd.get(), &boot, sizeof(boot))) { + if (lseek(fd.get(), static_cast<off_t>(offset), SEEK_SET) != static_cast<off_t>(offset)) { + *err = android::base::StringPrintf("failed to lseek %s: %s", misc_blk_device.c_str(), + strerror(errno)); + return false; + } + if (!android::base::WriteFully(fd.get(), p, size)) { *err = android::base::StringPrintf("failed to write %s: %s", misc_blk_device.c_str(), strerror(errno)); return false; } + // TODO: O_SYNC and fsync duplicates each other? if (fsync(fd.get()) == -1) { *err = android::base::StringPrintf("failed to fsync %s: %s", misc_blk_device.c_str(), @@ -82,6 +138,14 @@ static bool write_bootloader_message(const bootloader_message& boot, std::string return true; } +bool read_bootloader_message(bootloader_message* boot, std::string* err) { + return read_misc_partition(boot, sizeof(*boot), BOOTLOADER_MESSAGE_OFFSET_IN_MISC, err); +} + +bool write_bootloader_message(const bootloader_message& boot, std::string* err) { + return write_misc_partition(&boot, sizeof(boot), BOOTLOADER_MESSAGE_OFFSET_IN_MISC, err); +} + bool clear_bootloader_message(std::string* err) { bootloader_message boot = {}; return write_bootloader_message(boot, err); @@ -100,6 +164,16 @@ bool write_bootloader_message(const std::vector<std::string>& options, std::stri return write_bootloader_message(boot, err); } +bool read_wipe_package(std::string* package_data, size_t size, std::string* err) { + package_data->resize(size); + return read_misc_partition(&(*package_data)[0], size, WIPE_PACKAGE_OFFSET_IN_MISC, err); +} + +bool write_wipe_package(const std::string& package_data, std::string* err) { + return write_misc_partition(package_data.data(), package_data.size(), + WIPE_PACKAGE_OFFSET_IN_MISC, err); +} + extern "C" bool write_bootloader_message(const char* options) { std::string err; return write_bootloader_message({options}, &err); diff --git a/bootloader_message/include/bootloader_message/bootloader_message.h b/bootloader_message/include/bootloader_message/bootloader_message.h new file mode 100644 index 000000000..f343c64ac --- /dev/null +++ b/bootloader_message/include/bootloader_message/bootloader_message.h @@ -0,0 +1,199 @@ +/* + * Copyright (C) 2008 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef _BOOTLOADER_MESSAGE_H +#define _BOOTLOADER_MESSAGE_H + +#include <assert.h> +#include <stddef.h> +#include <stdint.h> + +// Spaces used by misc partition are as below: +// 0 - 2K For bootloader_message +// 2K - 16K Used by Vendor's bootloader (the 2K - 4K range may be optionally used +// as bootloader_message_ab struct) +// 16K - 64K Used by uncrypt and recovery to store wipe_package for A/B devices +// Note that these offsets are admitted by bootloader,recovery and uncrypt, so they +// are not configurable without changing all of them. +static const size_t BOOTLOADER_MESSAGE_OFFSET_IN_MISC = 0; +static const size_t WIPE_PACKAGE_OFFSET_IN_MISC = 16 * 1024; + +/* Bootloader Message (2-KiB) + * + * This structure describes the content of a block in flash + * that is used for recovery and the bootloader to talk to + * each other. + * + * The command field is updated by linux when it wants to + * reboot into recovery or to update radio or bootloader firmware. + * It is also updated by the bootloader when firmware update + * is complete (to boot into recovery for any final cleanup) + * + * The status field is written by the bootloader after the + * completion of an "update-radio" or "update-hboot" command. + * + * The recovery field is only written by linux and used + * for the system to send a message to recovery or the + * other way around. + * + * The stage field is written by packages which restart themselves + * multiple times, so that the UI can reflect which invocation of the + * package it is. If the value is of the format "#/#" (eg, "1/3"), + * the UI will add a simple indicator of that status. + * + * We used to have slot_suffix field for A/B boot control metadata in + * this struct, which gets unintentionally cleared by recovery or + * uncrypt. Move it into struct bootloader_message_ab to avoid the + * issue. + */ +struct bootloader_message { + char command[32]; + char status[32]; + char recovery[768]; + + // The 'recovery' field used to be 1024 bytes. It has only ever + // been used to store the recovery command line, so 768 bytes + // should be plenty. We carve off the last 256 bytes to store the + // stage string (for multistage packages) and possible future + // expansion. + char stage[32]; + + // The 'reserved' field used to be 224 bytes when it was initially + // carved off from the 1024-byte recovery field. Bump it up to + // 1184-byte so that the entire bootloader_message struct rounds up + // to 2048-byte. + char reserved[1184]; +}; + +/** + * We must be cautious when changing the bootloader_message struct size, + * because A/B-specific fields may end up with different offsets. + */ +#if (__STDC_VERSION__ >= 201112L) || defined(__cplusplus) +static_assert(sizeof(struct bootloader_message) == 2048, + "struct bootloader_message size changes, which may break A/B devices"); +#endif + +/** + * The A/B-specific bootloader message structure (4-KiB). + * + * We separate A/B boot control metadata from the regular bootloader + * message struct and keep it here. Everything that's A/B-specific + * stays after struct bootloader_message, which should be managed by + * the A/B-bootloader or boot control HAL. + * + * The slot_suffix field is used for A/B implementations where the + * bootloader does not set the androidboot.ro.boot.slot_suffix kernel + * commandline parameter. This is used by fs_mgr to mount /system and + * other partitions with the slotselect flag set in fstab. A/B + * implementations are free to use all 32 bytes and may store private + * data past the first NUL-byte in this field. It is encouraged, but + * not mandatory, to use 'struct bootloader_control' described below. + */ +struct bootloader_message_ab { + struct bootloader_message message; + char slot_suffix[32]; + + // Round up the entire struct to 4096-byte. + char reserved[2016]; +}; + +/** + * Be cautious about the struct size change, in case we put anything post + * bootloader_message_ab struct (b/29159185). + */ +#if (__STDC_VERSION__ >= 201112L) || defined(__cplusplus) +static_assert(sizeof(struct bootloader_message_ab) == 4096, + "struct bootloader_message_ab size changes"); +#endif + +#define BOOT_CTRL_MAGIC 0x42414342 /* Bootloader Control AB */ +#define BOOT_CTRL_VERSION 1 + +struct slot_metadata { + // Slot priority with 15 meaning highest priority, 1 lowest + // priority and 0 the slot is unbootable. + uint8_t priority : 4; + // Number of times left attempting to boot this slot. + uint8_t tries_remaining : 3; + // 1 if this slot has booted successfully, 0 otherwise. + uint8_t successful_boot : 1; + // 1 if this slot is corrupted from a dm-verity corruption, 0 + // otherwise. + uint8_t verity_corrupted : 1; + // Reserved for further use. + uint8_t reserved : 7; +} __attribute__((packed)); + +/* Bootloader Control AB + * + * This struct can be used to manage A/B metadata. It is designed to + * be put in the 'slot_suffix' field of the 'bootloader_message' + * structure described above. It is encouraged to use the + * 'bootloader_control' structure to store the A/B metadata, but not + * mandatory. + */ +struct bootloader_control { + // NUL terminated active slot suffix. + char slot_suffix[4]; + // Bootloader Control AB magic number (see BOOT_CTRL_MAGIC). + uint32_t magic; + // Version of struct being used (see BOOT_CTRL_VERSION). + uint8_t version; + // Number of slots being managed. + uint8_t nb_slot : 3; + // Number of times left attempting to boot recovery. + uint8_t recovery_tries_remaining : 3; + // Ensure 4-bytes alignment for slot_info field. + uint8_t reserved0[2]; + // Per-slot information. Up to 4 slots. + struct slot_metadata slot_info[4]; + // Reserved for further use. + uint8_t reserved1[8]; + // CRC32 of all 28 bytes preceding this field (little endian + // format). + uint32_t crc32_le; +} __attribute__((packed)); + +#if (__STDC_VERSION__ >= 201112L) || defined(__cplusplus) +static_assert(sizeof(struct bootloader_control) == + sizeof(((struct bootloader_message_ab *)0)->slot_suffix), + "struct bootloader_control has wrong size"); +#endif + +#ifdef __cplusplus + +#include <string> +#include <vector> + +bool read_bootloader_message(bootloader_message* boot, std::string* err); +bool write_bootloader_message(const bootloader_message& boot, std::string* err); +bool write_bootloader_message(const std::vector<std::string>& options, std::string* err); +bool clear_bootloader_message(std::string* err); + +bool read_wipe_package(std::string* package_data, size_t size, std::string* err); +bool write_wipe_package(const std::string& package_data, std::string* err); + +#else + +#include <stdbool.h> + +// C Interface. +bool write_bootloader_message(const char* options); + +#endif // ifdef __cplusplus + +#endif // _BOOTLOADER_MESSAGE_H diff --git a/device.cpp b/device.cpp index f8fbb8a49..e717dddf7 100644 --- a/device.cpp +++ b/device.cpp @@ -16,36 +16,15 @@ #include "device.h" -#if defined(AB_OTA_UPDATER) - -static const char* MENU_ITEMS[] = { - "Reboot system now", - "Reboot to bootloader", - "Wipe data/factory reset", - "Mount /system", - "Run graphics test", - "Power off", - NULL, -}; - -static const Device::BuiltinAction MENU_ACTIONS[] = { - Device::REBOOT, - Device::REBOOT_BOOTLOADER, - Device::WIPE_DATA, - Device::MOUNT_SYSTEM, - Device::RUN_GRAPHICS_TEST, - Device::SHUTDOWN, -}; - -#else - static const char* MENU_ITEMS[] = { "Reboot system now", "Reboot to bootloader", "Apply update from ADB", "Apply update from SD card", "Wipe data/factory reset", +#ifndef AB_OTA_UPDATER "Wipe cache partition", +#endif // !AB_OTA_UPDATER "Mount /system", "View recovery logs", "Run graphics test", @@ -59,14 +38,19 @@ static const Device::BuiltinAction MENU_ACTIONS[] = { Device::APPLY_ADB_SIDELOAD, Device::APPLY_SDCARD, Device::WIPE_DATA, +#ifndef AB_OTA_UPDATER Device::WIPE_CACHE, +#endif // !AB_OTA_UPDATER Device::MOUNT_SYSTEM, Device::VIEW_RECOVERY_LOGS, Device::RUN_GRAPHICS_TEST, Device::SHUTDOWN, }; -#endif +static_assert(sizeof(MENU_ITEMS) / sizeof(MENU_ITEMS[0]) == + sizeof(MENU_ACTIONS) / sizeof(MENU_ACTIONS[0]) + 1, + "MENU_ITEMS and MENU_ACTIONS should have the same length, " + "except for the extra NULL entry in MENU_ITEMS."); const char* const* Device::GetMenuItems() { return MENU_ITEMS; diff --git a/error_code.h b/error_code.h index 09ea82019..dfea0eb38 100644 --- a/error_code.h +++ b/error_code.h @@ -21,7 +21,8 @@ enum ErrorCode { kNoError = -1, kLowBattery = 20, kZipVerificationFailure, - kZipOpenFailure + kZipOpenFailure, + kBootreasonInBlacklist }; enum CauseCode { @@ -45,7 +46,7 @@ enum CauseCode { enum UncryptErrorCode { kUncryptNoError = -1, - kUncryptErrorHolder = 50, + kUncryptErrorPlaceholder = 50, kUncryptTimeoutError = 100, kUncryptFileRemoveError, kUncryptFileOpenError, diff --git a/install.cpp b/install.cpp index 427bea1a7..83ddc4b0c 100644 --- a/install.cpp +++ b/install.cpp @@ -17,6 +17,7 @@ #include <ctype.h> #include <errno.h> #include <fcntl.h> +#include <inttypes.h> #include <limits.h> #include <string.h> #include <sys/stat.h> @@ -24,6 +25,8 @@ #include <unistd.h> #include <chrono> +#include <limits> +#include <map> #include <string> #include <vector> @@ -32,6 +35,8 @@ #include <android-base/parseint.h> #include <android-base/stringprintf.h> #include <android-base/strings.h> +#include <cutils/properties.h> +#include <android-base/logging.h> #include "common.h" #include "error_code.h" @@ -46,6 +51,8 @@ extern RecoveryUI* ui; #define ASSUMED_UPDATE_BINARY_NAME "META-INF/com/google/android/update-binary" +static constexpr const char* AB_OTA_PAYLOAD_PROPERTIES = "payload_properties.txt"; +static constexpr const char* AB_OTA_PAYLOAD = "payload.bin"; #define PUBLIC_KEYS_FILE "/res/keys" static constexpr const char* METADATA_PATH = "META-INF/com/android/metadata"; static constexpr const char* UNCRYPT_STATUS = "/cache/recovery/uncrypt_status"; @@ -71,20 +78,27 @@ static int parse_build_number(const std::string& str) { return -1; } -// Read the build.version.incremental of src/tgt from the metadata and log it to last_install. -static void read_source_target_build(ZipArchive* zip, std::vector<std::string>& log_buffer) { +bool read_metadata_from_package(ZipArchive* zip, std::string* meta_data) { const ZipEntry* meta_entry = mzFindZipEntry(zip, METADATA_PATH); if (meta_entry == nullptr) { LOG(ERROR) << "Failed to find " << METADATA_PATH << " in update package"; - return; + return false; } - std::string meta_data(meta_entry->uncompLen, '\0'); - if (!mzReadZipEntry(zip, meta_entry, &meta_data[0], meta_entry->uncompLen)) { + meta_data->resize(meta_entry->uncompLen, '\0'); + if (!mzReadZipEntry(zip, meta_entry, &(*meta_data)[0], meta_entry->uncompLen)) { LOG(ERROR) << "Failed to read metadata in update package"; - return; + return false; } + return true; +} +// Read the build.version.incremental of src/tgt from the metadata and log it to last_install. +static void read_source_target_build(ZipArchive* zip, std::vector<std::string>& log_buffer) { + std::string meta_data; + if (!read_metadata_from_package(zip, &meta_data)) { + return; + } // Examples of the pre-build and post-build strings in metadata: // pre-build-incremental=2943039 // post-build-incremental=2951741 @@ -107,17 +121,148 @@ static void read_source_target_build(ZipArchive* zip, std::vector<std::string>& } } -// If the package contains an update binary, extract it and run it. +// Extract the update binary from the open zip archive |zip| located at |path| +// and store into |cmd| the command line that should be called. The |status_fd| +// is the file descriptor the child process should use to report back the +// progress of the update. static int -try_update_binary(const char* path, ZipArchive* zip, bool* wipe_cache, - std::vector<std::string>& log_buffer, int retry_count) +update_binary_command(const char* path, ZipArchive* zip, int retry_count, + int status_fd, std::vector<std::string>* cmd); + +#ifdef AB_OTA_UPDATER + +// Parses the metadata of the OTA package in |zip| and checks whether we are +// allowed to accept this A/B package. Downgrading is not allowed unless +// explicitly enabled in the package and only for incremental packages. +static int check_newer_ab_build(ZipArchive* zip) { - read_source_target_build(zip, log_buffer); + std::string metadata_str; + if (!read_metadata_from_package(zip, &metadata_str)) { + return INSTALL_CORRUPT; + } + std::map<std::string, std::string> metadata; + for (const std::string& line : android::base::Split(metadata_str, "\n")) { + size_t eq = line.find('='); + if (eq != std::string::npos) { + metadata[line.substr(0, eq)] = line.substr(eq + 1); + } + } + char value[PROPERTY_VALUE_MAX]; + + property_get("ro.product.device", value, ""); + const std::string& pkg_device = metadata["pre-device"]; + if (pkg_device != value || pkg_device.empty()) { + LOG(ERROR) << "Package is for product " << pkg_device << " but expected " << value; + return INSTALL_ERROR; + } + + // We allow the package to not have any serialno, but if it has a non-empty + // value it should match. + property_get("ro.serialno", value, ""); + const std::string& pkg_serial_no = metadata["serialno"]; + if (!pkg_serial_no.empty() && pkg_serial_no != value) { + LOG(ERROR) << "Package is for serial " << pkg_serial_no; + return INSTALL_ERROR; + } + + if (metadata["ota-type"] != "AB") { + LOG(ERROR) << "Package is not A/B"; + return INSTALL_ERROR; + } + + // Incremental updates should match the current build. + property_get("ro.build.version.incremental", value, ""); + const std::string& pkg_pre_build = metadata["pre-build-incremental"]; + if (!pkg_pre_build.empty() && pkg_pre_build != value) { + LOG(ERROR) << "Package is for source build " << pkg_pre_build << " but expected " << value; + return INSTALL_ERROR; + } + property_get("ro.build.fingerprint", value, ""); + const std::string& pkg_pre_build_fingerprint = metadata["pre-build"]; + if (!pkg_pre_build_fingerprint.empty() && + pkg_pre_build_fingerprint != value) { + LOG(ERROR) << "Package is for source build " << pkg_pre_build_fingerprint + << " but expected " << value; + return INSTALL_ERROR; + } + + // Check for downgrade version. + int64_t build_timestamp = property_get_int64( + "ro.build.date.utc", std::numeric_limits<int64_t>::max()); + int64_t pkg_post_timestamp = 0; + // We allow to full update to the same version we are running, in case there + // is a problem with the current copy of that version. + if (metadata["post-timestamp"].empty() || + !android::base::ParseInt(metadata["post-timestamp"].c_str(), + &pkg_post_timestamp) || + pkg_post_timestamp < build_timestamp) { + if (metadata["ota-downgrade"] != "yes") { + LOG(ERROR) << "Update package is older than the current build, expected a build " + "newer than timestamp " << build_timestamp << " but package has " + "timestamp " << pkg_post_timestamp << " and downgrade not allowed."; + return INSTALL_ERROR; + } + if (pkg_pre_build_fingerprint.empty()) { + LOG(ERROR) << "Downgrade package must have a pre-build version set, not allowed."; + return INSTALL_ERROR; + } + } + + return 0; +} + +static int +update_binary_command(const char* path, ZipArchive* zip, int retry_count, + int status_fd, std::vector<std::string>* cmd) +{ + int ret = check_newer_ab_build(zip); + if (ret) { + return ret; + } + + // For A/B updates we extract the payload properties to a buffer and obtain + // the RAW payload offset in the zip file. + const ZipEntry* properties_entry = + mzFindZipEntry(zip, AB_OTA_PAYLOAD_PROPERTIES); + if (!properties_entry) { + LOG(ERROR) << "Can't find " << AB_OTA_PAYLOAD_PROPERTIES; + return INSTALL_CORRUPT; + } + std::vector<unsigned char> payload_properties( + mzGetZipEntryUncompLen(properties_entry)); + if (!mzExtractZipEntryToBuffer(zip, properties_entry, + payload_properties.data())) { + LOG(ERROR) << "Can't extract " << AB_OTA_PAYLOAD_PROPERTIES; + return INSTALL_CORRUPT; + } + + const ZipEntry* payload_entry = mzFindZipEntry(zip, AB_OTA_PAYLOAD); + if (!payload_entry) { + LOG(ERROR) << "Can't find " << AB_OTA_PAYLOAD; + return INSTALL_CORRUPT; + } + long payload_offset = mzGetZipEntryOffset(payload_entry); + *cmd = { + "/sbin/update_engine_sideload", + android::base::StringPrintf("--payload=file://%s", path), + android::base::StringPrintf("--offset=%ld", payload_offset), + "--headers=" + std::string(payload_properties.begin(), + payload_properties.end()), + android::base::StringPrintf("--status_fd=%d", status_fd), + }; + return 0; +} + +#else // !AB_OTA_UPDATER +static int +update_binary_command(const char* path, ZipArchive* zip, int retry_count, + int status_fd, std::vector<std::string>* cmd) +{ + // On traditional updates we extract the update binary from the package. const ZipEntry* binary_entry = mzFindZipEntry(zip, ASSUMED_UPDATE_BINARY_NAME); if (binary_entry == NULL) { - mzCloseZipArchive(zip); return INSTALL_CORRUPT; } @@ -126,21 +271,47 @@ try_update_binary(const char* path, ZipArchive* zip, bool* wipe_cache, int fd = creat(binary, 0755); if (fd < 0) { PLOG(ERROR) << "Can't make " << binary; - mzCloseZipArchive(zip); return INSTALL_ERROR; } bool ok = mzExtractZipEntryToFile(zip, binary_entry, fd); close(fd); - mzCloseZipArchive(zip); if (!ok) { LOG(ERROR) << "Can't copy " << ASSUMED_UPDATE_BINARY_NAME; return INSTALL_ERROR; } + *cmd = { + binary, + EXPAND(RECOVERY_API_VERSION), // defined in Android.mk + std::to_string(status_fd), + path, + }; + if (retry_count > 0) + cmd->push_back("retry"); + return 0; +} +#endif // !AB_OTA_UPDATER + +// If the package contains an update binary, extract it and run it. +static int +try_update_binary(const char* path, ZipArchive* zip, bool* wipe_cache, + std::vector<std::string>& log_buffer, int retry_count) +{ + read_source_target_build(zip, log_buffer); + int pipefd[2]; pipe(pipefd); + std::vector<std::string> args; + int ret = update_binary_command(path, zip, retry_count, pipefd[1], &args); + mzCloseZipArchive(zip); + if (ret) { + close(pipefd[0]); + close(pipefd[1]); + return ret; + } + // When executing the update binary contained in the package, the // arguments passed are: // @@ -190,22 +361,27 @@ try_update_binary(const char* path, ZipArchive* zip, bool* wipe_cache, // update attempt. // - const char* args[6]; - args[0] = binary; - args[1] = EXPAND(RECOVERY_API_VERSION); // defined in Android.mk - char temp[16]; - snprintf(temp, sizeof(temp), "%d", pipefd[1]); - args[2] = temp; - args[3] = path; - args[4] = retry_count > 0 ? "retry" : NULL; - args[5] = NULL; + // Convert the vector to a NULL-terminated char* array suitable for execv. + const char* chr_args[args.size() + 1]; + chr_args[args.size()] = NULL; + for (size_t i = 0; i < args.size(); i++) { + chr_args[i] = args[i].c_str(); + } pid_t pid = fork(); + + if (pid == -1) { + close(pipefd[0]); + close(pipefd[1]); + PLOG(ERROR) << "Failed to fork update binary"; + return INSTALL_ERROR; + } + if (pid == 0) { umask(022); close(pipefd[0]); - execv(binary, const_cast<char**>(args)); - fprintf(stdout, "E:Can't run %s (%s)\n", binary, strerror(errno)); + execv(chr_args[0], const_cast<char**>(chr_args)); + fprintf(stdout, "E:Can't run %s (%s)\n", chr_args[0], strerror(errno)); _exit(-1); } close(pipefd[1]); @@ -301,33 +477,16 @@ really_install_package(const char *path, bool* wipe_cache, bool needs_mount, return INSTALL_CORRUPT; } - // Load keys. - std::vector<Certificate> loadedKeys; - if (!load_keys(PUBLIC_KEYS_FILE, loadedKeys)) { - LOG(ERROR) << "Failed to load keys"; - sysReleaseMap(&map); - return INSTALL_CORRUPT; - } - LOG(INFO) << loadedKeys.size() << " key(s) loaded from " << PUBLIC_KEYS_FILE; - // Verify package. - ui->Print("Verifying update package...\n"); - - auto t0 = std::chrono::system_clock::now(); - int err = verify_file(map.addr, map.length, loadedKeys); - std::chrono::duration<double> duration = std::chrono::system_clock::now() - t0; - ui->Print("Update package verification took %.1f s (result %d).\n", duration.count(), err); - if (err != VERIFY_SUCCESS) { - LOG(ERROR) << "signature verification failed"; + if (!verify_package(map.addr, map.length)) { log_buffer.push_back(android::base::StringPrintf("error: %d", kZipVerificationFailure)); - sysReleaseMap(&map); return INSTALL_CORRUPT; } // Try to open the package. ZipArchive zip; - err = mzOpenZipArchive(map.addr, map.length, &zip); + int err = mzOpenZipArchive(map.addr, map.length, &zip); if (err != 0) { LOG(ERROR) << "Can't open " << path; log_buffer.push_back(android::base::StringPrintf("error: %d", kZipOpenFailure)); @@ -377,7 +536,7 @@ install_package(const char* path, bool* wipe_cache, const char* install_file, std::string uncrypt_status; if (!android::base::ReadFileToString(UNCRYPT_STATUS, &uncrypt_status)) { PLOG(WARNING) << "failed to read uncrypt status"; - } else if (!android::base::StartsWith(uncrypt_status, "uncrypt_:")) { + } else if (!android::base::StartsWith(uncrypt_status, "uncrypt_")) { PLOG(WARNING) << "corrupted uncrypt_status: " << uncrypt_status; } else { log_buffer.push_back(android::base::Trim(uncrypt_status)); @@ -402,3 +561,25 @@ install_package(const char* path, bool* wipe_cache, const char* install_file, return result; } + +bool verify_package(const unsigned char* package_data, size_t package_size) { + std::vector<Certificate> loadedKeys; + if (!load_keys(PUBLIC_KEYS_FILE, loadedKeys)) { + LOG(ERROR) << "Failed to load keys"; + return false; + } + LOG(INFO) << loadedKeys.size() << " key(s) loaded from " << PUBLIC_KEYS_FILE; + + // Verify package. + ui->Print("Verifying update package...\n"); + auto t0 = std::chrono::system_clock::now(); + int err = verify_file(const_cast<unsigned char*>(package_data), package_size, loadedKeys); + std::chrono::duration<double> duration = std::chrono::system_clock::now() - t0; + ui->Print("Update package verification took %.1f s (result %d).\n", duration.count(), err); + if (err != VERIFY_SUCCESS) { + LOG(ERROR) << "Signature verification failed"; + LOG(ERROR) << "error: " << kZipVerificationFailure; + return false; + } + return true; +} @@ -17,11 +17,10 @@ #ifndef RECOVERY_INSTALL_H_ #define RECOVERY_INSTALL_H_ -#include "common.h" +#include <string> -#ifdef __cplusplus -extern "C" { -#endif +#include "common.h" +#include "minzip/Zip.h" enum { INSTALL_SUCCESS, INSTALL_ERROR, INSTALL_CORRUPT, INSTALL_NONE, INSTALL_SKIPPED, INSTALL_RETRY }; @@ -31,8 +30,12 @@ enum { INSTALL_SUCCESS, INSTALL_ERROR, INSTALL_CORRUPT, INSTALL_NONE, INSTALL_SK int install_package(const char* root_path, bool* wipe_cache, const char* install_file, bool needs_mount, int retry_count); -#ifdef __cplusplus -} -#endif +// Verify the package by ota keys. Return true if the package is verified successfully, +// otherwise return false. +bool verify_package(const unsigned char* package_data, size_t package_size); + +// Read meta data file of the package, write its content in the string pointed by meta_data. +// Return true if succeed, otherwise return false. +bool read_metadata_from_package(ZipArchive* zip, std::string* meta_data); #endif // RECOVERY_INSTALL_H_ diff --git a/minui/font_10x18.h b/minui/font_10x18.h index 29d705344..30dfb9c56 100644 --- a/minui/font_10x18.h +++ b/minui/font_10x18.h @@ -1,14 +1,14 @@ struct { unsigned width; unsigned height; - unsigned cwidth; - unsigned cheight; + unsigned char_width; + unsigned char_height; unsigned char rundata[2973]; } font = { .width = 960, .height = 18, - .cwidth = 10, - .cheight = 18, + .char_width = 10, + .char_height = 18, .rundata = { 0x7f,0x7f,0x7f,0x7f,0x7f,0x7f,0x7f,0x55,0x82,0x06,0x82,0x02,0x82,0x10,0x82, 0x11,0x83,0x08,0x82,0x0a,0x82,0x04,0x82,0x46,0x82,0x08,0x82,0x07,0x84,0x06, diff --git a/minui/graphics.cpp b/minui/graphics.cpp index c0eea9e38..ab56a6fd6 100644 --- a/minui/graphics.cpp +++ b/minui/graphics.cpp @@ -35,12 +35,6 @@ #include "minui.h" #include "graphics.h" -struct GRFont { - GRSurface* texture; - int cwidth; - int cheight; -}; - static GRFont* gr_font = NULL; static minui_backend* gr_backend = NULL; @@ -60,15 +54,20 @@ static bool outside(int x, int y) return x < 0 || x >= gr_draw->width || y < 0 || y >= gr_draw->height; } -int gr_measure(const char *s) +const GRFont* gr_sys_font() +{ + return gr_font; +} + +int gr_measure(const GRFont* font, const char *s) { - return gr_font->cwidth * strlen(s); + return font->char_width * strlen(s); } -void gr_font_size(int *x, int *y) +void gr_font_size(const GRFont* font, int *x, int *y) { - *x = gr_font->cwidth; - *y = gr_font->cheight; + *x = font->char_width; + *y = font->char_height; } static void text_blend(unsigned char* src_p, int src_row_bytes, @@ -103,34 +102,32 @@ static void text_blend(unsigned char* src_p, int src_row_bytes, } } -void gr_text(int x, int y, const char *s, bool bold) +void gr_text(const GRFont* font, int x, int y, const char *s, bool bold) { - GRFont* font = gr_font; - if (!font->texture || gr_current_a == 0) return; - bold = bold && (font->texture->height != font->cheight); + bold = bold && (font->texture->height != font->char_height); x += overscan_offset_x; y += overscan_offset_y; unsigned char ch; while ((ch = *s++)) { - if (outside(x, y) || outside(x+font->cwidth-1, y+font->cheight-1)) break; + if (outside(x, y) || outside(x+font->char_width-1, y+font->char_height-1)) break; if (ch < ' ' || ch > '~') { ch = '?'; } - unsigned char* src_p = font->texture->data + ((ch - ' ') * font->cwidth) + - (bold ? font->cheight * font->texture->row_bytes : 0); + unsigned char* src_p = font->texture->data + ((ch - ' ') * font->char_width) + + (bold ? font->char_height * font->texture->row_bytes : 0); unsigned char* dst_p = gr_draw->data + y*gr_draw->row_bytes + x*gr_draw->pixel_bytes; text_blend(src_p, font->texture->row_bytes, dst_p, gr_draw->row_bytes, - font->cwidth, font->cheight); + font->char_width, font->char_height); - x += font->cwidth; + x += font->char_width; } } @@ -267,40 +264,59 @@ unsigned int gr_get_height(GRSurface* surface) { return surface->height; } +int gr_init_font(const char* name, GRFont** dest) { + GRFont* font = reinterpret_cast<GRFont*>(calloc(1, sizeof(*gr_font))); + if (font == nullptr) { + return -1; + } + + int res = res_create_alpha_surface(name, &(font->texture)); + if (res < 0) { + free(font); + return res; + } + + // The font image should be a 96x2 array of character images. The + // columns are the printable ASCII characters 0x20 - 0x7f. The + // top row is regular text; the bottom row is bold. + font->char_width = font->texture->width / 96; + font->char_height = font->texture->height / 2; + + *dest = font; + + return 0; +} + static void gr_init_font(void) { - gr_font = reinterpret_cast<GRFont*>(calloc(sizeof(*gr_font), 1)); - - int res = res_create_alpha_surface("font", &(gr_font->texture)); + int res = gr_init_font("font", &gr_font); if (res == 0) { - // The font image should be a 96x2 array of character images. The - // columns are the printable ASCII characters 0x20 - 0x7f. The - // top row is regular text; the bottom row is bold. - gr_font->cwidth = gr_font->texture->width / 96; - gr_font->cheight = gr_font->texture->height / 2; - } else { - printf("failed to read font: res=%d\n", res); - - // fall back to the compiled-in font. - gr_font->texture = reinterpret_cast<GRSurface*>(malloc(sizeof(*gr_font->texture))); - gr_font->texture->width = font.width; - gr_font->texture->height = font.height; - gr_font->texture->row_bytes = font.width; - gr_font->texture->pixel_bytes = 1; - - unsigned char* bits = reinterpret_cast<unsigned char*>(malloc(font.width * font.height)); - gr_font->texture->data = reinterpret_cast<unsigned char*>(bits); - - unsigned char data; - unsigned char* in = font.rundata; - while((data = *in++)) { - memset(bits, (data & 0x80) ? 255 : 0, data & 0x7f); - bits += (data & 0x7f); - } + return; + } + + printf("failed to read font: res=%d\n", res); + - gr_font->cwidth = font.cwidth; - gr_font->cheight = font.cheight; + // fall back to the compiled-in font. + gr_font = reinterpret_cast<GRFont*>(calloc(1, sizeof(*gr_font))); + gr_font->texture = reinterpret_cast<GRSurface*>(malloc(sizeof(*gr_font->texture))); + gr_font->texture->width = font.width; + gr_font->texture->height = font.height; + gr_font->texture->row_bytes = font.width; + gr_font->texture->pixel_bytes = 1; + + unsigned char* bits = reinterpret_cast<unsigned char*>(malloc(font.width * font.height)); + gr_font->texture->data = reinterpret_cast<unsigned char*>(bits); + + unsigned char data; + unsigned char* in = font.rundata; + while((data = *in++)) { + memset(bits, (data & 0x80) ? 255 : 0, data & 0x7f); + bits += (data & 0x7f); } + + gr_font->char_width = font.char_width; + gr_font->char_height = font.char_height; } #if 0 diff --git a/minui/minui.h b/minui/minui.h index 5362d3fe3..78890b84b 100644 --- a/minui/minui.h +++ b/minui/minui.h @@ -33,6 +33,12 @@ struct GRSurface { unsigned char* data; }; +struct GRFont { + GRSurface* texture; + int char_width; + int char_height; +}; + int gr_init(); void gr_exit(); @@ -45,10 +51,14 @@ void gr_fb_blank(bool blank); void gr_clear(); // clear entire surface to current color void gr_color(unsigned char r, unsigned char g, unsigned char b, unsigned char a); void gr_fill(int x1, int y1, int x2, int y2); -void gr_text(int x, int y, const char *s, bool bold); + void gr_texticon(int x, int y, GRSurface* icon); -int gr_measure(const char *s); -void gr_font_size(int *x, int *y); + +const GRFont* gr_sys_font(); +int gr_init_font(const char* name, GRFont** dest); +void gr_text(const GRFont* font, int x, int y, const char *s, bool bold); +int gr_measure(const GRFont* font, const char *s); +void gr_font_size(const GRFont* font, int *x, int *y); void gr_blit(GRSurface* source, int sx, int sy, int w, int h, int dx, int dy); unsigned int gr_get_width(GRSurface* surface); diff --git a/minzip/SysUtil.cpp b/minzip/SysUtil.cpp index 2936c5ca4..4cdd60d9f 100644 --- a/minzip/SysUtil.cpp +++ b/minzip/SysUtil.cpp @@ -118,10 +118,10 @@ static int sysMapBlockFile(FILE* mapf, MemMapping* pMap) break; } size_t length = (end - start) * blksize; - if (end <= start || (end - start) > SIZE_MAX / blksize || length > remaining_size) { - LOG(ERROR) << "unexpected range in block map: " << start << " " << end; - success = false; - break; + if (end <= start || ((end - start) > SIZE_MAX / blksize) || length > remaining_size) { + LOG(ERROR) << "unexpected range in block map: " << start << " " << end; + success = false; + break; } void* addr = mmap64(next, length, PROT_READ, MAP_PRIVATE | MAP_FIXED, fd, ((off64_t)start)*blksize); @@ -137,8 +137,8 @@ static int sysMapBlockFile(FILE* mapf, MemMapping* pMap) remaining_size -= length; } if (success && remaining_size != 0) { - LOG(ERROR) << "ranges in block map are invalid: remaining_size = " << remaining_size; - success = false; + LOG(ERROR) << "ranges in block map are invalid: remaining_size = " << remaining_size; + success = false; } if (!success) { close(fd); diff --git a/recovery.cpp b/recovery.cpp index 9cf63c420..3b1305e61 100644 --- a/recovery.cpp +++ b/recovery.cpp @@ -46,6 +46,7 @@ #include <android-base/stringprintf.h> #include <android-base/strings.h> #include <android-base/unique_fd.h> +#include <bootloader_message/bootloader_message.h> #include <cutils/android_reboot.h> #include <cutils/properties.h> /* for property_list */ #include <healthd/BatteryMonitor.h> @@ -54,7 +55,6 @@ #include <selinux/selinux.h> #include "adb_install.h" -#include "bootloader.h" #include "common.h" #include "device.h" #include "error_code.h" @@ -64,6 +64,7 @@ #include "minadbd/minadbd.h" #include "minui/minui.h" #include "minzip/DirUtil.h" +#include "minzip/Zip.h" #include "roots.h" #include "ui.h" #include "screen_ui.h" @@ -84,10 +85,17 @@ static const struct option OPTIONS[] = { { "shutdown_after", no_argument, NULL, 'p' }, { "reason", required_argument, NULL, 'r' }, { "security", no_argument, NULL, 'e'}, - { "brick", no_argument, NULL, 0 }, + { "wipe_ab", no_argument, NULL, 0 }, + { "wipe_package_size", required_argument, NULL, 0 }, { NULL, 0, NULL, 0 }, }; +// More bootreasons can be found in "system/core/bootstat/bootstat.cpp". +static const std::vector<std::string> bootreason_blacklist { + "kernel_panic", + "Panic", +}; + static const char *CACHE_LOG_DIR = "/cache/recovery"; static const char *COMMAND_FILE = "/cache/recovery/command"; static const char *LOG_FILE = "/cache/recovery/log"; @@ -111,7 +119,7 @@ static const int BATTERY_READ_TIMEOUT_IN_SEC = 10; // So we should check battery with a slightly lower limitation. static const int BATTERY_OK_PERCENTAGE = 20; static const int BATTERY_WITH_CHARGER_OK_PERCENTAGE = 15; -constexpr const char* RECOVERY_BRICK = "/etc/recovery.brick"; +constexpr const char* RECOVERY_WIPE = "/etc/recovery.wipe"; RecoveryUI* ui = NULL; static const char* locale = "en_US"; @@ -300,9 +308,13 @@ static void redirect_stdio(const char* filename) { // - the contents of COMMAND_FILE (one per line) static void get_args(int *argc, char ***argv) { - struct bootloader_message boot; - memset(&boot, 0, sizeof(boot)); - get_bootloader_message(&boot); // this may fail, leaving a zeroed structure + bootloader_message boot = {}; + std::string err; + if (!read_bootloader_message(&boot, &err)) { + LOG(ERROR) << err; + // If fails, leave a zeroed bootloader_message. + memset(&boot, 0, sizeof(boot)); + } stage = strndup(boot.stage, sizeof(boot.stage)); if (boot.command[0] != 0) { @@ -367,16 +379,20 @@ get_args(int *argc, char ***argv) { strlcat(boot.recovery, (*argv)[i], sizeof(boot.recovery)); strlcat(boot.recovery, "\n", sizeof(boot.recovery)); } - set_bootloader_message(&boot); + if (!write_bootloader_message(boot, &err)) { + LOG(ERROR) << err; + } } static void set_sdcard_update_bootloader_message() { - struct bootloader_message boot; - memset(&boot, 0, sizeof(boot)); + bootloader_message boot = {}; strlcpy(boot.command, "boot-recovery", sizeof(boot.command)); strlcpy(boot.recovery, "recovery\n", sizeof(boot.recovery)); - set_bootloader_message(&boot); + std::string err; + if (!write_bootloader_message(boot, &err)) { + LOG(ERROR) << err; + } } // Read from kernel log into buffer and write out to file. @@ -527,9 +543,11 @@ finish_recovery() { copy_logs(); // Reset to normal system boot so recovery won't cycle indefinitely. - struct bootloader_message boot; - memset(&boot, 0, sizeof(boot)); - set_bootloader_message(&boot); + bootloader_message boot = {}; + std::string err; + if (!write_bootloader_message(boot, &err)) { + LOG(ERROR) << err; + } // Remove the command file, so recovery won't repeat indefinitely. if (has_cache) { @@ -897,15 +915,79 @@ static bool secure_wipe_partition(const std::string& partition) { return true; } -// Brick the current device, with a secure wipe of all the partitions in -// RECOVERY_BRICK. -static bool brick_device() { +// Check if the wipe package matches expectation: +// 1. verify the package. +// 2. check metadata (ota-type, pre-device and serial number if having one). +static bool check_wipe_package(size_t wipe_package_size) { + if (wipe_package_size == 0) { + LOG(ERROR) << "wipe_package_size is zero"; + return false; + } + std::string wipe_package; + std::string err_str; + if (!read_wipe_package(&wipe_package, wipe_package_size, &err_str)) { + PLOG(ERROR) << "Failed to read wipe package"; + return false; + } + if (!verify_package(reinterpret_cast<const unsigned char*>(wipe_package.data()), + wipe_package.size())) { + LOG(ERROR) << "Failed to verify package"; + return false; + } + + // Extract metadata + ZipArchive zip; + int err = mzOpenZipArchive(reinterpret_cast<unsigned char*>(&wipe_package[0]), + wipe_package.size(), &zip); + if (err != 0) { + LOG(ERROR) << "Can't open wipe package"; + return false; + } + std::string metadata; + if (!read_metadata_from_package(&zip, &metadata)) { + mzCloseZipArchive(&zip); + return false; + } + mzCloseZipArchive(&zip); + + // Check metadata + std::vector<std::string> lines = android::base::Split(metadata, "\n"); + bool ota_type_matched = false; + bool device_type_matched = false; + bool has_serial_number = false; + bool serial_number_matched = false; + for (const auto& line : lines) { + if (line == "ota-type=BRICK") { + ota_type_matched = true; + } else if (android::base::StartsWith(line, "pre-device=")) { + std::string device_type = line.substr(strlen("pre-device=")); + char real_device_type[PROPERTY_VALUE_MAX]; + property_get("ro.build.product", real_device_type, ""); + device_type_matched = (device_type == real_device_type); + } else if (android::base::StartsWith(line, "serialno=")) { + std::string serial_no = line.substr(strlen("serialno=")); + char real_serial_no[PROPERTY_VALUE_MAX]; + property_get("ro.serialno", real_serial_no, ""); + has_serial_number = true; + serial_number_matched = (serial_no == real_serial_no); + } + } + return ota_type_matched && device_type_matched && (!has_serial_number || serial_number_matched); +} + +// Wipe the current A/B device, with a secure wipe of all the partitions in +// RECOVERY_WIPE. +static bool wipe_ab_device(size_t wipe_package_size) { ui->SetBackground(RecoveryUI::ERASING); ui->SetProgressType(RecoveryUI::INDETERMINATE); + if (!check_wipe_package(wipe_package_size)) { + LOG(ERROR) << "Failed to verify wipe package"; + return false; + } std::string partition_list; - if (!android::base::ReadFileToString(RECOVERY_BRICK, &partition_list)) { - LOG(ERROR) << "failed to read \"" << RECOVERY_BRICK << "\""; + if (!android::base::ReadFileToString(RECOVERY_WIPE, &partition_list)) { + LOG(ERROR) << "failed to read \"" << RECOVERY_WIPE << "\""; return false; } @@ -1310,7 +1392,7 @@ static bool is_battery_ok() { } static void set_retry_bootloader_message(int retry_count, int argc, char** argv) { - struct bootloader_message boot {}; + bootloader_message boot = {}; strlcpy(boot.command, "boot-recovery", sizeof(boot.command)); strlcpy(boot.recovery, "recovery\n", sizeof(boot.recovery)); @@ -1329,7 +1411,37 @@ static void set_retry_bootloader_message(int retry_count, int argc, char** argv) snprintf(buffer, sizeof(buffer), "--retry_count=%d\n", retry_count+1); strlcat(boot.recovery, buffer, sizeof(boot.recovery)); } - set_bootloader_message(&boot); + std::string err; + if (!write_bootloader_message(boot, &err)) { + LOG(ERROR) << err; + } +} + +static bool bootreason_in_blacklist() { + char bootreason[PROPERTY_VALUE_MAX]; + if (property_get("ro.boot.bootreason", bootreason, nullptr) > 0) { + for (const auto& str : bootreason_blacklist) { + if (strcasecmp(str.c_str(), bootreason) == 0) { + return true; + } + } + } + return false; +} + +static void log_failure_code(ErrorCode code, const char *update_package) { + std::vector<std::string> log_buffer = { + update_package, + "0", // install result + "error: " + std::to_string(code), + }; + std::string log_content = android::base::Join(log_buffer, "\n"); + if (!android::base::WriteStringToFile(log_content, TEMPORARY_INSTALL_FILE)) { + PLOG(ERROR) << "failed to write " << TEMPORARY_INSTALL_FILE; + } + + // Also write the info into last_log. + LOG(INFO) << log_content; } static ssize_t logbasename( @@ -1427,7 +1539,8 @@ int main(int argc, char **argv) { const char *update_package = NULL; bool should_wipe_data = false; bool should_wipe_cache = false; - bool should_brick = false; + bool should_wipe_ab = false; + size_t wipe_package_size = 0; bool show_text = false; bool sideload = false; bool sideload_auto_reboot = false; @@ -1461,8 +1574,11 @@ int main(int argc, char **argv) { case 'r': reason = optarg; break; case 'e': security_update = true; break; case 0: { - if (strcmp(OPTIONS[option_index].name, "brick") == 0) { - should_brick = true; + if (strcmp(OPTIONS[option_index].name, "wipe_ab") == 0) { + should_wipe_ab = true; + break; + } else if (strcmp(OPTIONS[option_index].name, "wipe_package_size") == 0) { + android::base::ParseUint(optarg, &wipe_package_size); break; } break; @@ -1553,19 +1669,12 @@ int main(int argc, char **argv) { BATTERY_OK_PERCENTAGE); // Log the error code to last_install when installation skips due to // low battery. - std::vector<std::string> log_buffer = { - update_package, - "0", // install result - "error: " + std::to_string(kLowBattery), - }; - std::string log_content = android::base::Join(log_buffer, "\n"); - if (!android::base::WriteStringToFile(log_content, LAST_INSTALL_FILE)) { - PLOG(ERROR) << "failed to write " << LAST_INSTALL_FILE; - } - - // Also write the info into last_log. - LOG(INFO) << log_content; - + log_failure_code(kLowBattery, update_package); + status = INSTALL_SKIPPED; + } else if (bootreason_in_blacklist()) { + // Skip update-on-reboot when bootreason is kernel_panic or similar + ui->Print("bootreason is in the blacklist; skip OTA installation\n"); + log_failure_code(kBootreasonInBlacklist, update_package); status = INSTALL_SKIPPED; } else { status = install_package(update_package, &should_wipe_cache, @@ -1608,8 +1717,8 @@ int main(int argc, char **argv) { if (!wipe_cache(false, device)) { status = INSTALL_ERROR; } - } else if (should_brick) { - if (!brick_device()) { + } else if (should_wipe_ab) { + if (!wipe_ab_device(wipe_package_size)) { status = INSTALL_ERROR; } } else if (sideload) { diff --git a/screen_ui.cpp b/screen_ui.cpp index c61727258..a7b03c50d 100644 --- a/screen_ui.cpp +++ b/screen_ui.cpp @@ -54,8 +54,6 @@ static double now() { ScreenRecoveryUI::ScreenRecoveryUI() : currentIcon(NONE), locale(nullptr), - intro_done(false), - current_frame(0), progressBarType(EMPTY), progressScopeStart(0), progressScopeSize(0), @@ -76,6 +74,8 @@ ScreenRecoveryUI::ScreenRecoveryUI() : file_viewer_text_(nullptr), intro_frames(0), loop_frames(0), + current_frame(0), + intro_done(false), animation_fps(30), // TODO: there's currently no way to infer this. stage(-1), max_stage(-1), @@ -106,29 +106,41 @@ int ScreenRecoveryUI::PixelsFromDp(int dp) { // Here's the intended layout: -// | regular large -// ---------+-------------------- -// | 220dp 366dp -// icon | (200dp) (200dp) -// | 68dp 68dp -// text | (14sp) (14sp) -// | 32dp 32dp -// progress | (2dp) (2dp) -// | 194dp 340dp +// | portrait large landscape large +// ---------+------------------------------------------------- +// gap | 220dp 366dp 142dp 284dp +// icon | (200dp) +// gap | 68dp 68dp 56dp 112dp +// text | (14sp) +// gap | 32dp 32dp 26dp 52dp +// progress | (2dp) +// gap | 194dp 340dp 131dp 262dp // Note that "baseline" is actually the *top* of each icon (because that's how our drawing // routines work), so that's the more useful measurement for calling code. +enum Layout { PORTRAIT = 0, PORTRAIT_LARGE = 1, LANDSCAPE = 2, LANDSCAPE_LARGE = 3, LAYOUT_MAX }; +enum Dimension { PROGRESS = 0, TEXT = 1, ICON = 2, DIMENSION_MAX }; +static constexpr int kLayouts[LAYOUT_MAX][DIMENSION_MAX] = { + { 194, 32, 68, }, // PORTRAIT + { 340, 32, 68, }, // PORTRAIT_LARGE + { 131, 26, 56, }, // LANDSCAPE + { 262, 52, 112, }, // LANDSCAPE_LARGE +}; + int ScreenRecoveryUI::GetAnimationBaseline() { - return GetTextBaseline() - PixelsFromDp(68) - gr_get_height(loopFrames[0]); + return GetTextBaseline() - PixelsFromDp(kLayouts[layout_][ICON]) - + gr_get_height(loopFrames[0]); } int ScreenRecoveryUI::GetTextBaseline() { - return GetProgressBaseline() - PixelsFromDp(32) - gr_get_height(installing_text); + return GetProgressBaseline() - PixelsFromDp(kLayouts[layout_][TEXT]) - + gr_get_height(installing_text); } int ScreenRecoveryUI::GetProgressBaseline() { - return gr_fb_height() - PixelsFromDp(is_large_ ? 340 : 194) - gr_get_height(progressBarFill); + return gr_fb_height() - PixelsFromDp(kLayouts[layout_][PROGRESS]) - + gr_get_height(progressBarFill); } // Clear the screen and draw the currently selected background icon (if any). @@ -248,7 +260,7 @@ void ScreenRecoveryUI::DrawHorizontalRule(int* y) { } void ScreenRecoveryUI::DrawTextLine(int x, int* y, const char* line, bool bold) { - gr_text(x, *y, line, bold); + gr_text(gr_sys_font(), x, *y, line, bold); *y += char_height_ + 4; } @@ -304,10 +316,10 @@ void ScreenRecoveryUI::draw_screen_locked() { gr_fill(0, y - 2, gr_fb_width(), y + char_height_ + 2); // Bold white text for the selected item. SetColor(MENU_SEL_FG); - gr_text(4, y, menu_[i], true); + gr_text(gr_sys_font(), 4, y, menu_[i], true); SetColor(MENU); } else { - gr_text(4, y, menu_[i], false); + gr_text(gr_sys_font(), 4, y, menu_[i], false); } y += char_height_ + 4; } @@ -323,7 +335,7 @@ void ScreenRecoveryUI::draw_screen_locked() { for (int ty = gr_fb_height() - char_height_; ty >= y && count < text_rows_; ty -= char_height_, ++count) { - gr_text(0, ty, text_[row], false); + gr_text(gr_sys_font(), 0, ty, text_[row], false); --row; if (row < 0) row = text_rows_ - 1; } @@ -436,15 +448,24 @@ void ScreenRecoveryUI::SetSystemUpdateText(bool security_update) { Redraw(); } -void ScreenRecoveryUI::Init() { +void ScreenRecoveryUI::InitTextParams() { gr_init(); - density_ = static_cast<float>(android::base::GetIntProperty("ro.sf.lcd_density", 160)) / 160.f; - is_large_ = gr_fb_height() > PixelsFromDp(800); - - gr_font_size(&char_width_, &char_height_); + gr_font_size(gr_sys_font(), &char_width_, &char_height_); text_rows_ = gr_fb_height() / char_height_; text_cols_ = gr_fb_width() / char_width_; +} + +void ScreenRecoveryUI::Init() { + RecoveryUI::Init(); + InitTextParams(); + + density_ = static_cast<float>(android::base::GetIntProperty("ro.sf.lcd_density", 160)) / 160.f; + + // Are we portrait or landscape? + layout_ = (gr_fb_width() > gr_fb_height()) ? LANDSCAPE : PORTRAIT; + // Are we the large variant of our base layout? + if (gr_fb_height() > PixelsFromDp(800)) ++layout_; text_ = Alloc2d(text_rows_, text_cols_ + 1); file_viewer_text_ = Alloc2d(text_rows_, text_cols_ + 1); @@ -472,37 +493,42 @@ void ScreenRecoveryUI::Init() { LoadAnimation(); pthread_create(&progress_thread_, nullptr, ProgressThreadStartRoutine, this); - - RecoveryUI::Init(); } void ScreenRecoveryUI::LoadAnimation() { - // How many frames of intro and loop do we have? std::unique_ptr<DIR, decltype(&closedir)> dir(opendir("/res/images"), closedir); dirent* de; + std::vector<std::string> intro_frame_names; + std::vector<std::string> loop_frame_names; + while ((de = readdir(dir.get())) != nullptr) { - int value; - if (sscanf(de->d_name, "intro%d", &value) == 1 && intro_frames < (value + 1)) { - intro_frames = value + 1; - } else if (sscanf(de->d_name, "loop%d", &value) == 1 && loop_frames < (value + 1)) { - loop_frames = value + 1; + int value, num_chars; + if (sscanf(de->d_name, "intro%d%n.png", &value, &num_chars) == 1) { + intro_frame_names.emplace_back(de->d_name, num_chars); + } else if (sscanf(de->d_name, "loop%d%n.png", &value, &num_chars) == 1) { + loop_frame_names.emplace_back(de->d_name, num_chars); } } + intro_frames = intro_frame_names.size(); + loop_frames = loop_frame_names.size(); + // It's okay to not have an intro. if (intro_frames == 0) intro_done = true; // But you must have an animation. if (loop_frames == 0) abort(); + std::sort(intro_frame_names.begin(), intro_frame_names.end()); + std::sort(loop_frame_names.begin(), loop_frame_names.end()); + introFrames = new GRSurface*[intro_frames]; - for (int i = 0; i < intro_frames; ++i) { - // TODO: remember the names above, so we don't have to hard-code the number of 0s. - LoadBitmap(android::base::StringPrintf("intro%05d", i).c_str(), &introFrames[i]); + for (size_t i = 0; i < intro_frames; i++) { + LoadBitmap(intro_frame_names.at(i).c_str(), &introFrames[i]); } loopFrames = new GRSurface*[loop_frames]; - for (int i = 0; i < loop_frames; ++i) { - LoadBitmap(android::base::StringPrintf("loop%05d", i).c_str(), &loopFrames[i]); + for (size_t i = 0; i < loop_frames; i++) { + LoadBitmap(loop_frame_names.at(i).c_str(), &loopFrames[i]); } } diff --git a/screen_ui.h b/screen_ui.h index 4319b76ce..de7b6442e 100644 --- a/screen_ui.h +++ b/screen_ui.h @@ -37,16 +37,16 @@ class ScreenRecoveryUI : public RecoveryUI { void SetSystemUpdateText(bool security_update); // progress indicator - void SetProgressType(ProgressType type); - void ShowProgress(float portion, float seconds); - void SetProgress(float fraction); + void SetProgressType(ProgressType type) override; + void ShowProgress(float portion, float seconds) override; + void SetProgress(float fraction) override; - void SetStage(int current, int max); + void SetStage(int current, int max) override; // text log - void ShowText(bool visible); - bool IsTextVisible(); - bool WasTextEverVisible(); + void ShowText(bool visible) override; + bool IsTextVisible() override; + bool WasTextEverVisible() override; // printing messages void Print(const char* fmt, ...) __printflike(2, 3); @@ -72,13 +72,11 @@ class ScreenRecoveryUI : public RecoveryUI { Icon currentIcon; const char* locale; - bool intro_done; - int current_frame; // The scale factor from dp to pixels. 1.0 for mdpi, 4.0 for xxxhdpi. float density_; - // True if we should use the large layout. - bool is_large_; + // The layout to use. + int layout_; GRSurface* error_icon; @@ -123,8 +121,11 @@ class ScreenRecoveryUI : public RecoveryUI { pthread_t progress_thread_; // Number of intro frames and loop frames in the animation. - int intro_frames; - int loop_frames; + size_t intro_frames; + size_t loop_frames; + + size_t current_frame; + bool intro_done; // Number of frames per sec (default: 30) for both parts of the animation. int animation_fps; @@ -136,11 +137,13 @@ class ScreenRecoveryUI : public RecoveryUI { pthread_mutex_t updateMutex; bool rtl_locale; - void draw_background_locked(); - void draw_foreground_locked(); - void draw_screen_locked(); - void update_screen_locked(); - void update_progress_locked(); + virtual void InitTextParams(); + + virtual void draw_background_locked(); + virtual void draw_foreground_locked(); + virtual void draw_screen_locked(); + virtual void update_screen_locked(); + virtual void update_progress_locked(); GRSurface* GetCurrentFrame(); GRSurface* GetCurrentText(); @@ -148,8 +151,8 @@ class ScreenRecoveryUI : public RecoveryUI { static void* ProgressThreadStartRoutine(void* data); void ProgressThreadLoop(); - void ShowFile(FILE*); - void PrintV(const char*, bool, va_list); + virtual void ShowFile(FILE*); + virtual void PrintV(const char*, bool, va_list); void PutChar(char); void ClearText(); @@ -158,9 +161,9 @@ class ScreenRecoveryUI : public RecoveryUI { void LoadLocalizedBitmap(const char* filename, GRSurface** surface); int PixelsFromDp(int dp); - int GetAnimationBaseline(); - int GetProgressBaseline(); - int GetTextBaseline(); + virtual int GetAnimationBaseline(); + virtual int GetProgressBaseline(); + virtual int GetTextBaseline(); void DrawHorizontalRule(int* y); void DrawTextLine(int x, int* y, const char* line, bool bold); diff --git a/uncrypt/Android.mk b/uncrypt/Android.mk index 361379971..97fc70896 100644 --- a/uncrypt/Android.mk +++ b/uncrypt/Android.mk @@ -15,23 +15,12 @@ LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) -LOCAL_CLANG := true -LOCAL_SRC_FILES := bootloader_message_writer.cpp -LOCAL_MODULE := libbootloader_message_writer -LOCAL_STATIC_LIBRARIES := libbase libfs_mgr -LOCAL_CFLAGS := -Werror -LOCAL_C_INCLUDES := $(LOCAL_PATH)/.. -LOCAL_EXPORT_C_INCLUDE_DIRS := $(LOCAL_PATH)/include - -include $(BUILD_STATIC_LIBRARY) - -include $(CLEAR_VARS) LOCAL_CLANG := true LOCAL_SRC_FILES := uncrypt.cpp LOCAL_C_INCLUDES := $(LOCAL_PATH)/.. LOCAL_MODULE := uncrypt -LOCAL_STATIC_LIBRARIES := libbootloader_message_writer \ +LOCAL_STATIC_LIBRARIES := libbootloader_message \ libbase \ liblog \ libfs_mgr \ diff --git a/uncrypt/include/bootloader_message_writer.h b/uncrypt/include/bootloader_message_writer.h deleted file mode 100644 index e0ca3f44a..000000000 --- a/uncrypt/include/bootloader_message_writer.h +++ /dev/null @@ -1,35 +0,0 @@ -/* - * Copyright (C) 2016 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef BOOTLOADER_MESSAGE_WRITER_H -#define BOOTLOADER_MESSAGE_WRITER_H - -#ifdef __cplusplus -#include <string> -#include <vector> - -bool clear_bootloader_message(std::string* err); - -bool write_bootloader_message(const std::vector<std::string>& options, std::string* err); - -#else -#include <stdbool.h> - -// C Interface. -bool write_bootloader_message(const char* options); -#endif - -#endif // BOOTLOADER_MESSAGE_WRITER_H diff --git a/uncrypt/uncrypt.cpp b/uncrypt/uncrypt.cpp index c77e987b5..f31d55aa8 100644 --- a/uncrypt/uncrypt.cpp +++ b/uncrypt/uncrypt.cpp @@ -111,7 +111,7 @@ #include <android-base/stringprintf.h> #include <android-base/strings.h> #include <android-base/unique_fd.h> -#include <bootloader_message_writer.h> +#include <bootloader_message/bootloader_message.h> #include <cutils/android_reboot.h> #include <cutils/sockets.h> #include <fs_mgr.h> @@ -452,22 +452,23 @@ static int uncrypt(const char* input_path, const char* map_file, const int socke return 0; } -static bool uncrypt_wrapper(const char* input_path, const char* map_file, const int socket) { - // Initialize the uncrypt error to kUncryptErrorHolder. +static void log_uncrypt_error_code(UncryptErrorCode error_code) { if (!android::base::WriteStringToFile(android::base::StringPrintf( - "uncrypt_error: %d\n", kUncryptErrorHolder), UNCRYPT_STATUS)) { + "uncrypt_error: %d\n", error_code), UNCRYPT_STATUS)) { PLOG(WARNING) << "failed to write to " << UNCRYPT_STATUS; } +} + +static bool uncrypt_wrapper(const char* input_path, const char* map_file, const int socket) { + // Initialize the uncrypt error to kUncryptErrorPlaceholder. + log_uncrypt_error_code(kUncryptErrorPlaceholder); std::string package; if (input_path == nullptr) { if (!find_uncrypt_package(UNCRYPT_PATH_FILE, &package)) { write_status_to_socket(-1, socket); // Overwrite the error message. - if (!android::base::WriteStringToFile(android::base::StringPrintf( - "uncrypt_error: %d\n", kUncryptPackageMissingError), UNCRYPT_STATUS)) { - PLOG(WARNING) << "failed to write to " << UNCRYPT_STATUS; - } + log_uncrypt_error_code(kUncryptPackageMissingError); return false; } input_path = package.c_str(); @@ -528,14 +529,31 @@ static bool setup_bcb(const int socket) { return false; } LOG(INFO) << " received command: [" << content << "] (" << content.size() << ")"; + std::vector<std::string> options = android::base::Split(content, "\n"); + std::string wipe_package; + for (auto& option : options) { + if (android::base::StartsWith(option, "--wipe_package=")) { + std::string path = option.substr(strlen("--wipe_package=")); + if (!android::base::ReadFileToString(path, &wipe_package)) { + PLOG(ERROR) << "failed to read " << path; + return false; + } + option = android::base::StringPrintf("--wipe_package_size=%zu", wipe_package.size()); + } + } // c8. setup the bcb command std::string err; - if (!write_bootloader_message({content}, &err)) { + if (!write_bootloader_message(options, &err)) { LOG(ERROR) << "failed to set bootloader message: " << err; write_status_to_socket(-1, socket); return false; } + if (!wipe_package.empty() && !write_wipe_package(wipe_package, &err)) { + PLOG(ERROR) << "failed to set wipe package: " << err; + write_status_to_socket(-1, socket); + return false; + } // c10. send "100" status write_status_to_socket(100, socket); return true; @@ -569,10 +587,7 @@ int main(int argc, char** argv) { } if ((fstab = read_fstab()) == nullptr) { - if (!android::base::WriteStringToFile(android::base::StringPrintf( - "uncrypt_error: %d\n", kUncryptFstabReadError), UNCRYPT_STATUS)) { - PLOG(WARNING) << "failed to write to " << UNCRYPT_STATUS; - } + log_uncrypt_error_code(kUncryptFstabReadError); return 1; } @@ -581,30 +596,21 @@ int main(int argc, char** argv) { android::base::unique_fd service_socket(android_get_control_socket(UNCRYPT_SOCKET.c_str())); if (service_socket == -1) { PLOG(ERROR) << "failed to open socket \"" << UNCRYPT_SOCKET << "\""; - if (!android::base::WriteStringToFile(android::base::StringPrintf( - "uncrypt_error: %d\n", kUncryptSocketOpenError), UNCRYPT_STATUS)) { - PLOG(WARNING) << "failed to write to " << UNCRYPT_STATUS; - } + log_uncrypt_error_code(kUncryptSocketOpenError); return 1; } fcntl(service_socket, F_SETFD, FD_CLOEXEC); if (listen(service_socket, 1) == -1) { PLOG(ERROR) << "failed to listen on socket " << service_socket.get(); - if (!android::base::WriteStringToFile(android::base::StringPrintf( - "uncrypt_error: %d\n", kUncryptSocketListenError), UNCRYPT_STATUS)) { - PLOG(WARNING) << "failed to write to " << UNCRYPT_STATUS; - } + log_uncrypt_error_code(kUncryptSocketListenError); return 1; } android::base::unique_fd socket_fd(accept4(service_socket, nullptr, nullptr, SOCK_CLOEXEC)); if (socket_fd == -1) { PLOG(ERROR) << "failed to accept on socket " << service_socket.get(); - if (!android::base::WriteStringToFile(android::base::StringPrintf( - "uncrypt_error: %d\n", kUncryptSocketAcceptError), UNCRYPT_STATUS)) { - PLOG(WARNING) << "failed to write to " << UNCRYPT_STATUS; - } + log_uncrypt_error_code(kUncryptSocketAcceptError); return 1; } diff --git a/update_verifier/Android.mk b/update_verifier/Android.mk index 8f5194db6..090db998b 100644 --- a/update_verifier/Android.mk +++ b/update_verifier/Android.mk @@ -18,8 +18,11 @@ include $(CLEAR_VARS) LOCAL_CLANG := true LOCAL_SRC_FILES := update_verifier.cpp + LOCAL_MODULE := update_verifier -LOCAL_SHARED_LIBRARIES := libhardware libbase +LOCAL_SHARED_LIBRARIES := libbase libcutils libhardware liblog + +LOCAL_C_INCLUDES += $(LOCAL_PATH)/.. LOCAL_CFLAGS := -Werror include $(BUILD_EXECUTABLE) diff --git a/update_verifier/update_verifier.cpp b/update_verifier/update_verifier.cpp index 0a040c564..93ac605b1 100644 --- a/update_verifier/update_verifier.cpp +++ b/update_verifier/update_verifier.cpp @@ -22,22 +22,121 @@ * It relies on dm-verity to capture any corruption on the partitions being * verified. dm-verity must be in enforcing mode, so that it will reboot the * device on dm-verity failures. When that happens, the bootloader should - * mark the slot as unbootable and stops trying. We should never see a device - * started in dm-verity logging mode but with isSlotMarkedSuccessful equals to - * 0. + * mark the slot as unbootable and stops trying. Other dm-verity modes ( + * for example, veritymode=EIO) are not accepted and simply lead to a + * verification failure. * * The current slot will be marked as having booted successfully if the * verifier reaches the end after the verification. * - * TODO: The actual verification part will be added later after we have the - * A/B OTA package format in place. */ +#include <errno.h> +#include <fcntl.h> +#include <stdio.h> #include <string.h> +#include <string> +#include <vector> + +#include <android-base/file.h> #include <android-base/logging.h> +#include <android-base/parseint.h> +#include <android-base/strings.h> +#include <android-base/unique_fd.h> +#include <cutils/properties.h> #include <hardware/boot_control.h> +constexpr auto CARE_MAP_FILE = "/data/ota_package/care_map.txt"; +constexpr int BLOCKSIZE = 4096; + +static bool read_blocks(const std::string& blk_device_prefix, const std::string& range_str) { + char slot_suffix[PROPERTY_VALUE_MAX]; + property_get("ro.boot.slot_suffix", slot_suffix, ""); + std::string blk_device = blk_device_prefix + std::string(slot_suffix); + android::base::unique_fd fd(TEMP_FAILURE_RETRY(open(blk_device.c_str(), O_RDONLY))); + if (fd.get() == -1) { + PLOG(ERROR) << "Error reading partition " << blk_device; + return false; + } + + // For block range string, first integer 'count' equals 2 * total number of valid ranges, + // followed by 'count' number comma separated integers. Every two integers reprensent a + // block range with the first number included in range but second number not included. + // For example '4,64536,65343,74149,74150' represents: [64536,65343) and [74149,74150). + std::vector<std::string> ranges = android::base::Split(range_str, ","); + size_t range_count; + bool status = android::base::ParseUint(ranges[0].c_str(), &range_count); + if (!status || (range_count == 0) || (range_count % 2 != 0) || + (range_count != ranges.size()-1)) { + LOG(ERROR) << "Error in parsing range string."; + return false; + } + + size_t blk_count = 0; + for (size_t i = 1; i < ranges.size(); i += 2) { + unsigned int range_start, range_end; + bool parse_status = android::base::ParseUint(ranges[i].c_str(), &range_start); + parse_status = parse_status && android::base::ParseUint(ranges[i+1].c_str(), &range_end); + if (!parse_status || range_start >= range_end) { + LOG(ERROR) << "Invalid range pair " << ranges[i] << ", " << ranges[i+1]; + return false; + } + + if (lseek64(fd.get(), static_cast<off64_t>(range_start) * BLOCKSIZE, SEEK_SET) == -1) { + PLOG(ERROR) << "lseek to " << range_start << " failed"; + return false; + } + + size_t size = (range_end - range_start) * BLOCKSIZE; + std::vector<uint8_t> buf(size); + if (!android::base::ReadFully(fd.get(), buf.data(), size)) { + PLOG(ERROR) << "Failed to read blocks " << range_start << " to " << range_end; + return false; + } + blk_count += (range_end - range_start); + } + + LOG(INFO) << "Finished reading " << blk_count << " blocks on " << blk_device; + return true; +} + +static bool verify_image(const std::string& care_map_name) { + android::base::unique_fd care_map_fd(TEMP_FAILURE_RETRY(open(care_map_name.c_str(), O_RDONLY))); + // If the device is flashed before the current boot, it may not have care_map.txt + // in /data/ota_package. To allow the device to continue booting in this situation, + // we should print a warning and skip the block verification. + if (care_map_fd.get() == -1) { + LOG(WARNING) << "Warning: care map " << care_map_name << " not found."; + return true; + } + // Care map file has four lines (two lines if vendor partition is not present): + // First line has the block device name, e.g./dev/block/.../by-name/system. + // Second line holds all ranges of blocks to verify. + // The next two lines have the same format but for vendor partition. + std::string file_content; + if (!android::base::ReadFdToString(care_map_fd.get(), &file_content)) { + LOG(ERROR) << "Error reading care map contents to string."; + return false; + } + + std::vector<std::string> lines; + lines = android::base::Split(android::base::Trim(file_content), "\n"); + if (lines.size() != 2 && lines.size() != 4) { + LOG(ERROR) << "Invalid lines in care_map: found " << lines.size() + << " lines, expecting 2 or 4 lines."; + return false; + } + + for (size_t i = 0; i < lines.size(); i += 2) { + if (!read_blocks(lines[i], lines[i+1])) { + return false; + } + } + + return true; +} + int main(int argc, char** argv) { for (int i = 1; i < argc; i++) { LOG(INFO) << "Started with arg " << i << ": " << argv[i]; @@ -59,12 +158,22 @@ int main(int argc, char** argv) { if (is_successful == 0) { // The current slot has not booted successfully. - - // TODO: Add the actual verification after we have the A/B OTA package - // format in place. - - // TODO: Assert the dm-verity mode. Bootloader should never boot a newly - // flashed slot (isSlotMarkedSuccessful == 0) with dm-verity logging mode. + char verity_mode[PROPERTY_VALUE_MAX]; + if (property_get("ro.boot.veritymode", verity_mode, "") == -1) { + LOG(ERROR) << "Failed to get dm-verity mode."; + return -1; + } else if (strcasecmp(verity_mode, "eio") == 0) { + // We shouldn't see verity in EIO mode if the current slot hasn't booted + // successfully before. Therefore, fail the verification when veritymode=eio. + LOG(ERROR) << "Found dm-verity in EIO mode, skip verification."; + return -1; + } else if (strcmp(verity_mode, "enforcing") != 0) { + LOG(ERROR) << "Unexpected dm-verity mode : " << verity_mode << ", expecting enforcing."; + return -1; + } else if (!verify_image(CARE_MAP_FILE)) { + LOG(ERROR) << "Failed to verify all blocks in care map file."; + return -1; + } int ret = module->markBootSuccessful(module); if (ret != 0) { diff --git a/wear_ui.cpp b/wear_ui.cpp index 17889076a..0918ac457 100644 --- a/wear_ui.cpp +++ b/wear_ui.cpp @@ -47,32 +47,13 @@ static double now() { } WearRecoveryUI::WearRecoveryUI() : - progress_bar_height(3), - progress_bar_width(200), progress_bar_y(259), outer_height(0), outer_width(0), - menu_unusable_rows(0), - intro_frames(22), - loop_frames(60), - animation_fps(30), - currentIcon(NONE), - intro_done(false), - current_frame(0), - progressBarType(EMPTY), - progressScopeStart(0), - progressScopeSize(0), - progress(0), - text_cols(0), - text_rows(0), - text_col(0), - text_row(0), - text_top(0), - show_text(false), - show_text_ever(false), - show_menu(false), - menu_items(0), - menu_sel(0) { + menu_unusable_rows(0) { + intro_frames = 22; + loop_frames = 60; + animation_fps = 30; for (size_t i = 0; i < 5; i++) backgroundIcon[i] = NULL; @@ -80,16 +61,22 @@ WearRecoveryUI::WearRecoveryUI() : self = this; } +int WearRecoveryUI::GetProgressBaseline() { + return progress_bar_y; +} + // Draw background frame on the screen. Does not flip pages. // Should only be called with updateMutex locked. -void WearRecoveryUI::draw_background_locked(Icon icon) +// TODO merge drawing routines with screen_ui +void WearRecoveryUI::draw_background_locked() { + pagesIdentical = false; gr_color(0, 0, 0, 255); gr_fill(0, 0, gr_fb_width(), gr_fb_height()); - if (icon) { + if (currentIcon != NONE) { GRSurface* surface; - if (icon == INSTALLING_UPDATE || icon == ERASING) { + if (currentIcon == INSTALLING_UPDATE || currentIcon == ERASING) { if (!intro_done) { surface = introFrames[current_frame]; } else { @@ -97,7 +84,7 @@ void WearRecoveryUI::draw_background_locked(Icon icon) } } else { - surface = backgroundIcon[icon]; + surface = backgroundIcon[currentIcon]; } int width = gr_get_width(surface); @@ -110,36 +97,6 @@ void WearRecoveryUI::draw_background_locked(Icon icon) } } -// Draw the progress bar (if any) on the screen. Does not flip pages. -// Should only be called with updateMutex locked. -void WearRecoveryUI::draw_progress_locked() -{ - if (currentIcon == ERROR) return; - if (progressBarType != DETERMINATE) return; - - int width = progress_bar_width; - int height = progress_bar_height; - int dx = (gr_fb_width() - width)/2; - int dy = progress_bar_y; - - float p = progressScopeStart + progress * progressScopeSize; - int pos = (int) (p * width); - - gr_color(0x43, 0x43, 0x43, 0xff); - gr_fill(dx, dy, dx + width, dy + height); - - if (pos > 0) { - gr_color(0x02, 0xa8, 0xf3, 255); - if (rtl_locale) { - // Fill the progress bar from right to left. - gr_fill(dx + width - pos, dy, dx + width, dy + height); - } else { - // Fill the progress bar from left to right. - gr_fill(dx, dy, dx + pos, dy + height); - } - } -} - static const char* HEADERS[] = { "Swipe up/down to move.", "Swipe left/right to select.", @@ -147,13 +104,15 @@ static const char* HEADERS[] = { NULL }; +// TODO merge drawing routines with screen_ui void WearRecoveryUI::draw_screen_locked() { - draw_background_locked(currentIcon); - draw_progress_locked(); char cur_selection_str[50]; - if (show_text) { + draw_background_locked(); + if (!show_text) { + draw_foreground_locked(); + } else { SetColor(TEXT_FILL); gr_fill(0, 0, gr_fb_width(), gr_fb_height()); @@ -177,7 +136,7 @@ void WearRecoveryUI::draw_screen_locked() // items don't fit on the screen. if (menu_items > menu_end - menu_start) { sprintf(cur_selection_str, "Current item: %d/%d", menu_sel + 1, menu_items); - gr_text(x+4, y, cur_selection_str, 1); + gr_text(gr_sys_font(), x+4, y, cur_selection_str, 1); y += char_height_+4; } @@ -192,10 +151,12 @@ void WearRecoveryUI::draw_screen_locked() gr_fill(x, y-2, gr_fb_width()-x, y+char_height_+2); // white text of selected item SetColor(MENU_SEL_FG); - if (menu[i][0]) gr_text(x+4, y, menu[i], 1); + if (menu_[i][0]) { + gr_text(gr_sys_font(), x + 4, y, menu_[i], 1); + } SetColor(MENU); - } else { - if (menu[i][0]) gr_text(x+4, y, menu[i], 0); + } else if (menu_[i][0]) { + gr_text(gr_sys_font(), x + 4, y, menu_[i], 0); } y += char_height_+4; } @@ -211,163 +172,42 @@ void WearRecoveryUI::draw_screen_locked() // screen, the bottom of the menu, or we've displayed the // entire text buffer. int ty; - int row = (text_top+text_rows-1) % text_rows; + int row = (text_top_ + text_rows_ - 1) % text_rows_; size_t count = 0; for (int ty = gr_fb_height() - char_height_ - outer_height; - ty > y+2 && count < text_rows; + ty > y + 2 && count < text_rows_; ty -= char_height_, ++count) { - gr_text(x+4, ty, text[row], 0); + gr_text(gr_sys_font(), x+4, ty, text_[row], 0); --row; - if (row < 0) row = text_rows-1; + if (row < 0) row = text_rows_ - 1; } } } -void WearRecoveryUI::update_screen_locked() -{ +// TODO merge drawing routines with screen_ui +void WearRecoveryUI::update_progress_locked() { draw_screen_locked(); gr_flip(); } -// Keeps the progress bar updated, even when the process is otherwise busy. -void* WearRecoveryUI::progress_thread(void *cookie) { - self->progress_loop(); - return NULL; -} +void WearRecoveryUI::InitTextParams() { + ScreenRecoveryUI::InitTextParams(); -void WearRecoveryUI::progress_loop() { - double interval = 1.0 / animation_fps; - for (;;) { - double start = now(); - pthread_mutex_lock(&updateMutex); - int redraw = 0; + text_cols_ = (gr_fb_width() - (outer_width * 2)) / char_width_; - if ((currentIcon == INSTALLING_UPDATE || currentIcon == ERASING) - && !show_text) { - if (!intro_done) { - if (current_frame >= intro_frames - 1) { - intro_done = true; - current_frame = 0; - } else { - current_frame++; - } - } else { - current_frame = (current_frame + 1) % loop_frames; - } - redraw = 1; - } + if (text_rows_ > kMaxRows) text_rows_ = kMaxRows; + if (text_cols_ > kMaxCols) text_cols_ = kMaxCols; - // move the progress bar forward on timed intervals, if configured - int duration = progressScopeDuration; - if (progressBarType == DETERMINATE && duration > 0) { - double elapsed = now() - progressScopeTime; - float p = 1.0 * elapsed / duration; - if (p > 1.0) p = 1.0; - if (p > progress) { - progress = p; - redraw = 1; - } - } - - if (redraw) - update_screen_locked(); - - pthread_mutex_unlock(&updateMutex); - double end = now(); - // minimum of 20ms delay between frames - double delay = interval - (end-start); - if (delay < 0.02) delay = 0.02; - usleep(static_cast<useconds_t>(delay * 1000000)); - } -} - -void WearRecoveryUI::Init() -{ - gr_init(); - - gr_font_size(&char_width_, &char_height_); - - text_col = text_row = 0; - text_rows = (gr_fb_height()) / char_height_; visible_text_rows = (gr_fb_height() - (outer_height * 2)) / char_height_; - if (text_rows > kMaxRows) text_rows = kMaxRows; - text_top = 1; +} - text_cols = (gr_fb_width() - (outer_width * 2)) / char_width_; - if (text_cols > kMaxCols - 1) text_cols = kMaxCols - 1; +void WearRecoveryUI::Init() { + ScreenRecoveryUI::Init(); LoadBitmap("icon_installing", &backgroundIcon[INSTALLING_UPDATE]); backgroundIcon[ERASING] = backgroundIcon[INSTALLING_UPDATE]; LoadBitmap("icon_error", &backgroundIcon[ERROR]); backgroundIcon[NO_COMMAND] = backgroundIcon[ERROR]; - - introFrames = (GRSurface**)malloc(intro_frames * sizeof(GRSurface*)); - for (int i = 0; i < intro_frames; ++i) { - char filename[40]; - sprintf(filename, "intro%02d", i); - LoadBitmap(filename, introFrames + i); - } - - loopFrames = (GRSurface**)malloc(loop_frames * sizeof(GRSurface*)); - for (int i = 0; i < loop_frames; ++i) { - char filename[40]; - sprintf(filename, "loop%02d", i); - LoadBitmap(filename, loopFrames + i); - } - - pthread_create(&progress_t, NULL, progress_thread, NULL); - RecoveryUI::Init(); -} - -void WearRecoveryUI::SetBackground(Icon icon) -{ - pthread_mutex_lock(&updateMutex); - currentIcon = icon; - update_screen_locked(); - pthread_mutex_unlock(&updateMutex); -} - -void WearRecoveryUI::SetProgressType(ProgressType type) -{ - pthread_mutex_lock(&updateMutex); - if (progressBarType != type) { - progressBarType = type; - } - progressScopeStart = 0; - progressScopeSize = 0; - progress = 0; - update_screen_locked(); - pthread_mutex_unlock(&updateMutex); -} - -void WearRecoveryUI::ShowProgress(float portion, float seconds) -{ - pthread_mutex_lock(&updateMutex); - progressBarType = DETERMINATE; - progressScopeStart += progressScopeSize; - progressScopeSize = portion; - progressScopeTime = now(); - progressScopeDuration = seconds; - progress = 0; - update_screen_locked(); - pthread_mutex_unlock(&updateMutex); -} - -void WearRecoveryUI::SetProgress(float fraction) -{ - pthread_mutex_lock(&updateMutex); - if (fraction < 0.0) fraction = 0.0; - if (fraction > 1.0) fraction = 1.0; - if (progressBarType == DETERMINATE && fraction > progress) { - // Skip updates that aren't visibly different. - int width = progress_bar_width; - float scale = width * progressScopeSize; - if ((int) (progress * scale) != (int) (fraction * scale)) { - progress = fraction; - update_screen_locked(); - } - } - pthread_mutex_unlock(&updateMutex); } void WearRecoveryUI::SetStage(int current, int max) @@ -386,40 +226,40 @@ void WearRecoveryUI::Print(const char *fmt, ...) // This can get called before ui_init(), so be careful. pthread_mutex_lock(&updateMutex); - if (text_rows > 0 && text_cols > 0) { + if (text_rows_ > 0 && text_cols_ > 0) { char *ptr; for (ptr = buf; *ptr != '\0'; ++ptr) { - if (*ptr == '\n' || text_col >= text_cols) { - text[text_row][text_col] = '\0'; - text_col = 0; - text_row = (text_row + 1) % text_rows; - if (text_row == text_top) text_top = (text_top + 1) % text_rows; + if (*ptr == '\n' || text_col_ >= text_cols_) { + text_[text_row_][text_col_] = '\0'; + text_col_ = 0; + text_row_ = (text_row_ + 1) % text_rows_; + if (text_row_ == text_top_) text_top_ = (text_top_ + 1) % text_rows_; } - if (*ptr != '\n') text[text_row][text_col++] = *ptr; + if (*ptr != '\n') text_[text_row_][text_col_++] = *ptr; } - text[text_row][text_col] = '\0'; + text_[text_row_][text_col_] = '\0'; update_screen_locked(); } pthread_mutex_unlock(&updateMutex); } void WearRecoveryUI::StartMenu(const char* const * headers, const char* const * items, - int initial_selection) { + int initial_selection) { pthread_mutex_lock(&updateMutex); - if (text_rows > 0 && text_cols > 0) { + if (text_rows_ > 0 && text_cols_ > 0) { menu_headers_ = headers; size_t i = 0; - // "i < text_rows" is removed from the loop termination condition, + // "i < text_rows_" is removed from the loop termination condition, // which is different from the one in ScreenRecoveryUI::StartMenu(). // Because WearRecoveryUI supports scrollable menu, it's fine to have - // more entries than text_rows. The menu may be truncated otherwise. + // more entries than text_rows_. The menu may be truncated otherwise. // Bug: 23752519 for (; items[i] != nullptr; i++) { - strncpy(menu[i], items[i], text_cols - 1); - menu[i][text_cols - 1] = '\0'; + strncpy(menu_[i], items[i], text_cols_ - 1); + menu_[i][text_cols_ - 1] = '\0'; } menu_items = i; - show_menu = 1; + show_menu = true; menu_sel = initial_selection; menu_start = 0; menu_end = visible_text_rows - 1 - menu_unusable_rows; @@ -433,7 +273,7 @@ void WearRecoveryUI::StartMenu(const char* const * headers, const char* const * int WearRecoveryUI::SelectMenu(int sel) { int old_sel; pthread_mutex_lock(&updateMutex); - if (show_menu > 0) { + if (show_menu) { old_sel = menu_sel; menu_sel = sel; if (menu_sel < 0) menu_sel = 0; @@ -452,53 +292,6 @@ int WearRecoveryUI::SelectMenu(int sel) { return sel; } -void WearRecoveryUI::EndMenu() { - int i; - pthread_mutex_lock(&updateMutex); - if (show_menu > 0 && text_rows > 0 && text_cols > 0) { - show_menu = 0; - update_screen_locked(); - } - pthread_mutex_unlock(&updateMutex); -} - -bool WearRecoveryUI::IsTextVisible() -{ - pthread_mutex_lock(&updateMutex); - int visible = show_text; - pthread_mutex_unlock(&updateMutex); - return visible; -} - -bool WearRecoveryUI::WasTextEverVisible() -{ - pthread_mutex_lock(&updateMutex); - int ever_visible = show_text_ever; - pthread_mutex_unlock(&updateMutex); - return ever_visible; -} - -void WearRecoveryUI::ShowText(bool visible) -{ - pthread_mutex_lock(&updateMutex); - // Don't show text during ota install or factory reset - if (currentIcon == INSTALLING_UPDATE || currentIcon == ERASING) { - pthread_mutex_unlock(&updateMutex); - return; - } - show_text = visible; - if (show_text) show_text_ever = 1; - update_screen_locked(); - pthread_mutex_unlock(&updateMutex); -} - -void WearRecoveryUI::Redraw() -{ - pthread_mutex_lock(&updateMutex); - update_screen_locked(); - pthread_mutex_unlock(&updateMutex); -} - void WearRecoveryUI::ShowFile(FILE* fp) { std::vector<off_t> offsets; offsets.push_back(ftello(fp)); @@ -538,12 +331,12 @@ void WearRecoveryUI::ShowFile(FILE* fp) { int ch = getc(fp); if (ch == EOF) { - text_row = text_top = text_rows - 2; + text_row_ = text_top_ = text_rows_ - 2; show_prompt = true; } else { PutChar(ch); - if (text_col == 0 && text_row >= text_rows - 2) { - text_top = text_row; + if (text_col_ == 0 && text_row_ >= text_rows_ - 2) { + text_top_ = text_row_; show_prompt = true; } } @@ -552,10 +345,10 @@ void WearRecoveryUI::ShowFile(FILE* fp) { void WearRecoveryUI::PutChar(char ch) { pthread_mutex_lock(&updateMutex); - if (ch != '\n') text[text_row][text_col++] = ch; - if (ch == '\n' || text_col >= text_cols) { - text_col = 0; - ++text_row; + if (ch != '\n') text_[text_row_][text_col_++] = ch; + if (ch == '\n' || text_col_ >= text_cols_) { + text_col_ = 0; + ++text_row_; } pthread_mutex_unlock(&updateMutex); } @@ -572,11 +365,11 @@ void WearRecoveryUI::ShowFile(const char* filename) { void WearRecoveryUI::ClearText() { pthread_mutex_lock(&updateMutex); - text_col = 0; - text_row = 0; - text_top = 1; - for (size_t i = 0; i < text_rows; ++i) { - memset(text[i], 0, text_cols + 1); + text_col_ = 0; + text_row_ = 0; + text_top_ = 1; + for (size_t i = 0; i < text_rows_; ++i) { + memset(text_[i], 0, text_cols_ + 1); } pthread_mutex_unlock(&updateMutex); } @@ -597,17 +390,17 @@ void WearRecoveryUI::PrintV(const char* fmt, bool copy_to_stdout, va_list ap) { } pthread_mutex_lock(&updateMutex); - if (text_rows > 0 && text_cols > 0) { + if (text_rows_ > 0 && text_cols_ > 0) { for (const char* ptr = str.c_str(); *ptr != '\0'; ++ptr) { - if (*ptr == '\n' || text_col >= text_cols) { - text[text_row][text_col] = '\0'; - text_col = 0; - text_row = (text_row + 1) % text_rows; - if (text_row == text_top) text_top = (text_top + 1) % text_rows; + if (*ptr == '\n' || text_col_ >= text_cols_) { + text_[text_row_][text_col_] = '\0'; + text_col_ = 0; + text_row_ = (text_row_ + 1) % text_rows_; + if (text_row_ == text_top_) text_top_ = (text_top_ + 1) % text_rows_; } - if (*ptr != '\n') text[text_row][text_col++] = *ptr; + if (*ptr != '\n') text_[text_row_][text_col_++] = *ptr; } - text[text_row][text_col] = '\0'; + text_[text_row_][text_col_] = '\0'; update_screen_locked(); } pthread_mutex_unlock(&updateMutex); @@ -23,39 +23,22 @@ class WearRecoveryUI : public ScreenRecoveryUI { public: WearRecoveryUI(); - void Init(); - // overall recovery state ("background image") - void SetBackground(Icon icon); + void Init() override; - // progress indicator - void SetProgressType(ProgressType type); - void ShowProgress(float portion, float seconds); - void SetProgress(float fraction); - - void SetStage(int current, int max); - - // text log - void ShowText(bool visible); - bool IsTextVisible(); - bool WasTextEverVisible(); + void SetStage(int current, int max) override; // printing messages - void Print(const char* fmt, ...); - void PrintOnScreenOnly(const char* fmt, ...) __printflike(2, 3); - void ShowFile(const char* filename); - void ShowFile(FILE* fp); + void Print(const char* fmt, ...) override; + void PrintOnScreenOnly(const char* fmt, ...) override __printflike(2, 3); + void ShowFile(const char* filename) override; + void ShowFile(FILE* fp) override; // menu display void StartMenu(const char* const * headers, const char* const * items, - int initial_selection); - int SelectMenu(int sel); - void EndMenu(); - - void Redraw(); + int initial_selection) override; + int SelectMenu(int sel) override; protected: - int progress_bar_height, progress_bar_width; - // progress bar vertical position, it's centered horizontally int progress_bar_y; @@ -67,59 +50,34 @@ class WearRecoveryUI : public ScreenRecoveryUI { // that may otherwise go out of the screen. int menu_unusable_rows; - // number of intro frames (default: 22) and loop frames (default: 60) - int intro_frames; - int loop_frames; - - // Number of frames per sec (default: 30) for both of intro and loop. - int animation_fps; + int GetProgressBaseline() override; - private: - Icon currentIcon; + void InitTextParams() override; - bool intro_done; + void update_progress_locked() override; - int current_frame; + void PrintV(const char*, bool, va_list) override; + private: GRSurface* backgroundIcon[5]; - GRSurface* *introFrames; - GRSurface* *loopFrames; - - ProgressType progressBarType; - - float progressScopeStart, progressScopeSize, progress; - double progressScopeTime, progressScopeDuration; static const int kMaxCols = 96; static const int kMaxRows = 96; - // Log text overlay, displayed when a magic key is pressed - char text[kMaxRows][kMaxCols]; - size_t text_cols, text_rows; // Number of text rows seen on screen int visible_text_rows; - size_t text_col, text_row, text_top; - bool show_text; - bool show_text_ever; // has show_text ever been true? - char menu[kMaxRows][kMaxCols]; - bool show_menu; const char* const* menu_headers_; - int menu_items, menu_sel; int menu_start, menu_end; pthread_t progress_t; - private: - void draw_background_locked(Icon icon); + void draw_background_locked() override; + void draw_screen_locked() override; void draw_progress_locked(); - void draw_screen_locked(); - void update_screen_locked(); - static void* progress_thread(void* cookie); - void progress_loop(); + void PutChar(char); void ClearText(); - void PrintV(const char*, bool, va_list); }; #endif // RECOVERY_WEAR_UI_H |