From 8c7761edec5a85f0d544c4fa1b24f26d976fdb64 Mon Sep 17 00:00:00 2001 From: Tao Bao Date: Wed, 15 Nov 2017 11:45:23 -0800 Subject: [PATCH 001/560] minadbd: Move to Soong. Test: mmma -j bootable/recovery Change-Id: Ic4cd676a842f0e8c4735cc6731fcbf13b844510c --- Android.mk | 1 - minadbd/Android.bp | 78 ++++++++++++++++++++++++++++++++++++++++++++++ minadbd/Android.mk | 55 -------------------------------- 3 files changed, 78 insertions(+), 56 deletions(-) create mode 100644 minadbd/Android.bp delete mode 100644 minadbd/Android.mk diff --git a/Android.mk b/Android.mk index 2f57db3442..e8b899450f 100644 --- a/Android.mk +++ b/Android.mk @@ -259,7 +259,6 @@ include $(BUILD_STATIC_LIBRARY) include \ $(LOCAL_PATH)/boot_control/Android.mk \ - $(LOCAL_PATH)/minadbd/Android.mk \ $(LOCAL_PATH)/minui/Android.mk \ $(LOCAL_PATH)/tests/Android.mk \ $(LOCAL_PATH)/tools/Android.mk \ diff --git a/minadbd/Android.bp b/minadbd/Android.bp new file mode 100644 index 0000000000..fcdb482722 --- /dev/null +++ b/minadbd/Android.bp @@ -0,0 +1,78 @@ +// Copyright (C) 2018 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. + +cc_defaults { + name: "minadbd_defaults", + + cflags: [ + "-DADB_HOST=0", + "-Wall", + "-Werror", + ], + + include_dirs: [ + "bootable/recovery", + "system/core/adb", + ], +} + +cc_library_static { + name: "libminadbd", + + defaults: [ + "minadbd_defaults", + ], + + srcs: [ + "fuse_adb_provider.cpp", + "minadbd.cpp", + "minadbd_services.cpp", + ], + + static_libs: [ + "libbase", + "libcrypto", + ], + + whole_static_libs: [ + "libadbd", + ], +} + +cc_test { + name: "minadbd_test", + + defaults: [ + "minadbd_defaults", + ], + + srcs: [ + "fuse_adb_provider_test.cpp", + ], + + static_libs: [ + "libBionicGtestMain", + "libminadbd", + ], + + shared_libs: [ + "libbase", + "libcutils", + "liblog", + ], + + test_suites: [ + "device-tests", + ], +} diff --git a/minadbd/Android.mk b/minadbd/Android.mk deleted file mode 100644 index 50e3b34ef5..0000000000 --- a/minadbd/Android.mk +++ /dev/null @@ -1,55 +0,0 @@ -# Copyright 2005 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) - -minadbd_cflags := \ - -Wall -Werror \ - -DADB_HOST=0 \ - -# libminadbd (static library) -# =============================== -include $(CLEAR_VARS) - -LOCAL_SRC_FILES := \ - fuse_adb_provider.cpp \ - minadbd.cpp \ - minadbd_services.cpp \ - -LOCAL_MODULE := libminadbd -LOCAL_CFLAGS := $(minadbd_cflags) -LOCAL_C_INCLUDES := bootable/recovery system/core/adb -LOCAL_WHOLE_STATIC_LIBRARIES := libadbd -LOCAL_STATIC_LIBRARIES := libcrypto libbase - -include $(BUILD_STATIC_LIBRARY) - -# minadbd_test (native test) -# =============================== -include $(CLEAR_VARS) - -LOCAL_MODULE := minadbd_test -LOCAL_COMPATIBILITY_SUITE := device-tests -LOCAL_SRC_FILES := fuse_adb_provider_test.cpp -LOCAL_CFLAGS := $(minadbd_cflags) -LOCAL_C_INCLUDES := $(LOCAL_PATH) system/core/adb -LOCAL_STATIC_LIBRARIES := \ - libBionicGtestMain \ - libminadbd -LOCAL_SHARED_LIBRARIES := \ - liblog \ - libbase \ - libcutils - -include $(BUILD_NATIVE_TEST) From 4b51c3ab400eb8e02e21a6cd9e39b4800f4ab219 Mon Sep 17 00:00:00 2001 From: Tao Bao Date: Tue, 6 Mar 2018 22:05:25 -0800 Subject: [PATCH 002/560] Export fuse_sideload.h for libfusesideload. Move the module into a separate directory so that we can export only the expected header. With this change, minadbd no longer needs to include bootable/recovery. Test: mmma -j bootable/recovery Change-Id: I69b888ceb160a33a67d79c5bda208dc17ad6ed86 --- Android.mk | 12 ------ fuse_sideload/Android.bp | 37 +++++++++++++++++++ .../fuse_sideload.cpp | 0 .../include/fuse_sideload.h | 0 minadbd/Android.bp | 2 +- 5 files changed, 38 insertions(+), 13 deletions(-) create mode 100644 fuse_sideload/Android.bp rename fuse_sideload.cpp => fuse_sideload/fuse_sideload.cpp (100%) rename fuse_sideload.h => fuse_sideload/include/fuse_sideload.h (100%) diff --git a/Android.mk b/Android.mk index e8b899450f..ed7192967f 100644 --- a/Android.mk +++ b/Android.mk @@ -18,18 +18,6 @@ LOCAL_PATH := $(call my-dir) RECOVERY_API_VERSION := 3 RECOVERY_FSTAB_VERSION := 2 -# libfusesideload (static library) -# =============================== -include $(CLEAR_VARS) -LOCAL_SRC_FILES := fuse_sideload.cpp -LOCAL_CFLAGS := -Wall -Werror -LOCAL_CFLAGS += -D_XOPEN_SOURCE -D_GNU_SOURCE -LOCAL_MODULE := libfusesideload -LOCAL_STATIC_LIBRARIES := \ - libcrypto \ - libbase -include $(BUILD_STATIC_LIBRARY) - # libmounts (static library) # =============================== include $(CLEAR_VARS) diff --git a/fuse_sideload/Android.bp b/fuse_sideload/Android.bp new file mode 100644 index 0000000000..76bc16df96 --- /dev/null +++ b/fuse_sideload/Android.bp @@ -0,0 +1,37 @@ +// Copyright (C) 2018 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. + +cc_library_static { + name: "libfusesideload", + + cflags: [ + "-D_XOPEN_SOURCE", + "-D_GNU_SOURCE", + "-Wall", + "-Werror", + ], + + srcs: [ + "fuse_sideload.cpp", + ], + + export_include_dirs: [ + "include", + ], + + static_libs: [ + "libbase", + "libcrypto", + ], +} diff --git a/fuse_sideload.cpp b/fuse_sideload/fuse_sideload.cpp similarity index 100% rename from fuse_sideload.cpp rename to fuse_sideload/fuse_sideload.cpp diff --git a/fuse_sideload.h b/fuse_sideload/include/fuse_sideload.h similarity index 100% rename from fuse_sideload.h rename to fuse_sideload/include/fuse_sideload.h diff --git a/minadbd/Android.bp b/minadbd/Android.bp index fcdb482722..432b2f0f54 100644 --- a/minadbd/Android.bp +++ b/minadbd/Android.bp @@ -22,7 +22,6 @@ cc_defaults { ], include_dirs: [ - "bootable/recovery", "system/core/adb", ], } @@ -41,6 +40,7 @@ cc_library_static { ], static_libs: [ + "libfusesideload", "libbase", "libcrypto", ], From e8fdb1ab0b0b81c5590b8c5a9a362f2258efaf09 Mon Sep 17 00:00:00 2001 From: Tao Bao Date: Wed, 7 Mar 2018 09:54:50 -0800 Subject: [PATCH 003/560] Drop the no-op Android.bp. The "subdirs=" statement no longer has any effect (with the CL in [1] that's merged last November). All the Android.bp's will be picked up automatically. [1] commit 4f21237342e715cbbac6e409cd67e86a304483c4 in https://android-review.googlesource.com/c/platform/build/blueprint/+/530115. Test: mmma -j bootable/recovery Change-Id: Idd39af1fff907e8c1886f91eea0dd4a9e38a4079 --- Android.bp | 8 -------- 1 file changed, 8 deletions(-) delete mode 100644 Android.bp diff --git a/Android.bp b/Android.bp deleted file mode 100644 index f8c6a4b71f..0000000000 --- a/Android.bp +++ /dev/null @@ -1,8 +0,0 @@ -subdirs = [ - "applypatch", - "bootloader_message", - "edify", - "otafault", - "otautil", - "uncrypt", -] From 495c521ed32e92ed0e39a425fd5ce79c581dbcda Mon Sep 17 00:00:00 2001 From: Yifan Hong Date: Tue, 6 Mar 2018 17:59:58 -0800 Subject: [PATCH 004/560] recovery: add libhidl-gen-utils depedency introduced as a depedency to libvintf. Test: builds Bug: 73556059 Change-Id: Ia51ba81ef462879481dcacb80d9ea9ea35e8b0bb --- Android.mk | 1 + tests/Android.mk | 1 + 2 files changed, 2 insertions(+) diff --git a/Android.mk b/Android.mk index e8b899450f..510c66abef 100644 --- a/Android.mk +++ b/Android.mk @@ -167,6 +167,7 @@ LOCAL_STATIC_LIBRARIES := \ libcrypto \ libvintf_recovery \ libvintf \ + libhidl-gen-utils \ libtinyxml2 \ libbase \ libcutils \ diff --git a/tests/Android.mk b/tests/Android.mk index d911c25e4b..b3584fe87a 100644 --- a/tests/Android.mk +++ b/tests/Android.mk @@ -147,6 +147,7 @@ LOCAL_STATIC_LIBRARIES := \ libfs_mgr \ libvintf_recovery \ libvintf \ + libhidl-gen-utils \ libtinyxml2 \ libselinux \ libext4_utils \ From b4e3a370bf6fe2bbb6ad8e33d16ce3210595aaef Mon Sep 17 00:00:00 2001 From: Tianjie Xu Date: Thu, 8 Mar 2018 12:34:19 -0800 Subject: [PATCH 005/560] Set the update locations to default in CacheLocation's constructor Otherwise the applypatch executable will fail to back up the source file to /cache when patching the recovery image. Bug: 74198354 Test: run applypatch from boot to recovery Change-Id: I6e5b9cd06d6ed0b26066b779a348437ecf984b92 --- otautil/cache_location.cpp | 9 ++++----- otautil/include/otautil/cache_location.h | 5 +---- updater/updater.cpp | 4 ---- 3 files changed, 5 insertions(+), 13 deletions(-) diff --git a/otautil/cache_location.cpp b/otautil/cache_location.cpp index 8f289487f7..8ddefec5e9 100644 --- a/otautil/cache_location.cpp +++ b/otautil/cache_location.cpp @@ -25,8 +25,7 @@ CacheLocation& CacheLocation::location() { return cache_location; } -void CacheLocation::ResetLocations() { - cache_temp_source_ = kDefaultCacheTempSource; - last_command_file_ = kDefaultLastCommandFile; - stash_directory_base_ = kDefaultStashDirectoryBase; -} +CacheLocation::CacheLocation() + : cache_temp_source_(kDefaultCacheTempSource), + last_command_file_(kDefaultLastCommandFile), + stash_directory_base_(kDefaultStashDirectoryBase) {} diff --git a/otautil/include/otautil/cache_location.h b/otautil/include/otautil/cache_location.h index 85e0d485c5..f2f663816e 100644 --- a/otautil/include/otautil/cache_location.h +++ b/otautil/include/otautil/cache_location.h @@ -27,9 +27,6 @@ class CacheLocation { public: static CacheLocation& location(); - // Reset the locations to their default values. - void ResetLocations(); - // getter and setter functions. std::string cache_temp_source() const { return cache_temp_source_; @@ -53,7 +50,7 @@ class CacheLocation { } private: - CacheLocation() {} + CacheLocation(); DISALLOW_COPY_AND_ASSIGN(CacheLocation); // When there isn't enough room on the target filesystem to hold the patched version of the file, diff --git a/updater/updater.cpp b/updater/updater.cpp index f063e5fa18..1d6b172bb2 100644 --- a/updater/updater.cpp +++ b/updater/updater.cpp @@ -169,10 +169,6 @@ int main(int argc, char** argv) { } ota_io_init(za, state.is_retry); - // Initialize the cache_temp_source, last_command_file and stash_directory_base to their default - // locations. - CacheLocation::location().ResetLocations(); - std::string result; bool status = Evaluate(&state, root, &result); From d612b23dfd58dbe5059ba53d8fd13cbb343b177c Mon Sep 17 00:00:00 2001 From: Tao Bao Date: Mon, 12 Mar 2018 21:18:52 -0700 Subject: [PATCH 006/560] tests: Add ApplyPatchModesTest.PatchModeEmmcTargetWithBsdiffPatch test. /system/bin/applypatch on device is expected to work with bsdiff based recovery-from-boot patch automatically. Adding a test to ensure that's always the case. Bug: 72731506 Test: Run recovery_component_test on marlin. Change-Id: I56283cd3ce7cf0215cc3bb3619b206fa01d552c4 --- tests/component/applypatch_test.cpp | 51 +++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/tests/component/applypatch_test.cpp b/tests/component/applypatch_test.cpp index b6d0925579..61e06adb6a 100644 --- a/tests/component/applypatch_test.cpp +++ b/tests/component/applypatch_test.cpp @@ -30,6 +30,7 @@ #include #include #include +#include #include #include "applypatch/applypatch.h" @@ -38,6 +39,8 @@ #include "otautil/cache_location.h" #include "otautil/print_sha1.h" +using namespace std::string_literals; + static void sha1sum(const std::string& fname, std::string* sha1, size_t* fsize = nullptr) { ASSERT_NE(nullptr, sha1); @@ -265,6 +268,54 @@ TEST_F(ApplyPatchModesTest, PatchModeEmmcTarget) { ASSERT_EQ(0, applypatch_modes(args3.size(), args3.data())); } +// Ensures that applypatch works with a bsdiff based recovery-from-boot.p. +TEST_F(ApplyPatchModesTest, PatchModeEmmcTargetWithBsdiffPatch) { + std::string boot_img_file = from_testdata_base("boot.img"); + std::string boot_img_sha1; + size_t boot_img_size; + sha1sum(boot_img_file, &boot_img_sha1, &boot_img_size); + + std::string recovery_img_file = from_testdata_base("recovery.img"); + std::string recovery_img_sha1; + size_t recovery_img_size; + sha1sum(recovery_img_file, &recovery_img_sha1, &recovery_img_size); + + // Generate the bsdiff patch of recovery-from-boot.p. + std::string src_content; + ASSERT_TRUE(android::base::ReadFileToString(boot_img_file, &src_content)); + + std::string tgt_content; + ASSERT_TRUE(android::base::ReadFileToString(recovery_img_file, &tgt_content)); + + TemporaryFile patch_file; + ASSERT_EQ(0, + bsdiff::bsdiff(reinterpret_cast(src_content.data()), src_content.size(), + reinterpret_cast(tgt_content.data()), tgt_content.size(), + patch_file.path, nullptr)); + + // applypatch : + std::string src_file_arg = + "EMMC:" + boot_img_file + ":" + std::to_string(boot_img_size) + ":" + boot_img_sha1; + TemporaryFile tgt_file; + std::string tgt_file_arg = "EMMC:"s + tgt_file.path; + std::string recovery_img_size_arg = std::to_string(recovery_img_size); + std::string patch_arg = boot_img_sha1 + ":" + patch_file.path; + std::vector args = { "applypatch", + src_file_arg.c_str(), + tgt_file_arg.c_str(), + recovery_img_sha1.c_str(), + recovery_img_size_arg.c_str(), + patch_arg.c_str() }; + ASSERT_EQ(0, applypatch_modes(args.size(), args.data())); + + // Double check the patched recovery image. + std::string tgt_file_sha1; + size_t tgt_file_size; + sha1sum(tgt_file.path, &tgt_file_sha1, &tgt_file_size); + ASSERT_EQ(recovery_img_size, tgt_file_size); + ASSERT_EQ(recovery_img_sha1, tgt_file_sha1); +} + TEST_F(ApplyPatchModesTest, PatchModeInvalidArgs) { // Invalid bonus file. ASSERT_NE(0, applypatch_modes(3, (const char* []){ "applypatch", "-b", "/doesntexist" })); From e3499f902e4118a890404863f9d6f5d59c121220 Mon Sep 17 00:00:00 2001 From: Tao Bao Date: Tue, 20 Mar 2018 10:33:42 -0700 Subject: [PATCH 007/560] tests: Split ApplyPatchModesTest.PatchModeEmmcTarget. We have been seeing flakiness in continuous test, but unable to reproduce locally. Break it down into smaller units to narrow down the cause. Bug: 67849209 Test: Run recovery_component_test on marlin. Change-Id: Ia24b0c5d137bad27d502575fcd18d3ca9c9828b6 --- tests/component/applypatch_test.cpp | 119 +++++++++++++++++----------- 1 file changed, 74 insertions(+), 45 deletions(-) diff --git a/tests/component/applypatch_test.cpp b/tests/component/applypatch_test.cpp index 61e06adb6a..916028594c 100644 --- a/tests/component/applypatch_test.cpp +++ b/tests/component/applypatch_test.cpp @@ -205,67 +205,96 @@ TEST_F(ApplyPatchModesTest, PatchModeEmmcTarget) { sha1sum(boot_img, &boot_img_sha1, &boot_img_size); std::string recovery_img = from_testdata_base("recovery.img"); - size_t size; + size_t recovery_img_size; std::string recovery_img_sha1; - sha1sum(recovery_img, &recovery_img_sha1, &size); - std::string recovery_img_size = std::to_string(size); + sha1sum(recovery_img, &recovery_img_sha1, &recovery_img_size); + std::string recovery_img_size_arg = std::to_string(recovery_img_size); std::string bonus_file = from_testdata_base("bonus.file"); // applypatch -b : - TemporaryFile tmp1; - std::string src_file = + std::string src_file_arg = "EMMC:" + boot_img + ":" + std::to_string(boot_img_size) + ":" + boot_img_sha1; - std::string tgt_file = "EMMC:" + std::string(tmp1.path); - std::string patch = boot_img_sha1 + ":" + from_testdata_base("recovery-from-boot.p"); - std::vector args = { - "applypatch", - "-b", - bonus_file.c_str(), - src_file.c_str(), - tgt_file.c_str(), - recovery_img_sha1.c_str(), - recovery_img_size.c_str(), - patch.c_str() - }; + TemporaryFile tgt_file; + std::string tgt_file_arg = "EMMC:"s + tgt_file.path; + std::string patch_arg = boot_img_sha1 + ":" + from_testdata_base("recovery-from-boot.p"); + std::vector args = { "applypatch", + "-b", + bonus_file.c_str(), + src_file_arg.c_str(), + tgt_file_arg.c_str(), + recovery_img_sha1.c_str(), + recovery_img_size_arg.c_str(), + patch_arg.c_str() }; ASSERT_EQ(0, applypatch_modes(args.size(), args.data())); +} + +// Tests patching the EMMC target without a separate bonus file (i.e. recovery-from-boot patch has +// everything). +TEST_F(ApplyPatchModesTest, PatchModeEmmcTargetWithoutBonusFile) { + std::string boot_img = from_testdata_base("boot.img"); + size_t boot_img_size; + std::string boot_img_sha1; + sha1sum(boot_img, &boot_img_sha1, &boot_img_size); + + std::string recovery_img = from_testdata_base("recovery.img"); + size_t recovery_img_size; + std::string recovery_img_sha1; + sha1sum(recovery_img, &recovery_img_sha1, &recovery_img_size); + std::string recovery_img_size_arg = std::to_string(recovery_img_size); // applypatch : - TemporaryFile tmp2; - patch = boot_img_sha1 + ":" + from_testdata_base("recovery-from-boot-with-bonus.p"); - tgt_file = "EMMC:" + std::string(tmp2.path); - std::vector args2 = { - "applypatch", - src_file.c_str(), - tgt_file.c_str(), - recovery_img_sha1.c_str(), - recovery_img_size.c_str(), - patch.c_str() - }; - ASSERT_EQ(0, applypatch_modes(args2.size(), args2.data())); + std::string src_file_arg = + "EMMC:" + boot_img + ":" + std::to_string(boot_img_size) + ":" + boot_img_sha1; + TemporaryFile tgt_file; + std::string tgt_file_arg = "EMMC:"s + tgt_file.path; + std::string patch_arg = + boot_img_sha1 + ":" + from_testdata_base("recovery-from-boot-with-bonus.p"); + std::vector args = { "applypatch", + src_file_arg.c_str(), + tgt_file_arg.c_str(), + recovery_img_sha1.c_str(), + recovery_img_size_arg.c_str(), + patch_arg.c_str() }; + ASSERT_EQ(0, applypatch_modes(args.size(), args.data())); +} + +TEST_F(ApplyPatchModesTest, PatchModeEmmcTargetWithMultiplePatches) { + std::string boot_img = from_testdata_base("boot.img"); + size_t boot_img_size; + std::string boot_img_sha1; + sha1sum(boot_img, &boot_img_sha1, &boot_img_size); + + std::string recovery_img = from_testdata_base("recovery.img"); + size_t recovery_img_size; + std::string recovery_img_sha1; + sha1sum(recovery_img, &recovery_img_sha1, &recovery_img_size); + std::string recovery_img_size_arg = std::to_string(recovery_img_size); + + std::string bonus_file = from_testdata_base("bonus.file"); // applypatch -b \ - // : : - TemporaryFile tmp3; - tgt_file = "EMMC:" + std::string(tmp3.path); + // : : : + std::string src_file_arg = + "EMMC:" + boot_img + ":" + std::to_string(boot_img_size) + ":" + boot_img_sha1; + TemporaryFile tgt_file; + std::string tgt_file_arg = "EMMC:"s + tgt_file.path; std::string bad_sha1_a = android::base::StringPrintf("%040x", rand()); std::string bad_sha1_b = android::base::StringPrintf("%040x", rand()); std::string patch1 = bad_sha1_a + ":" + from_testdata_base("recovery-from-boot.p"); std::string patch2 = boot_img_sha1 + ":" + from_testdata_base("recovery-from-boot.p"); std::string patch3 = bad_sha1_b + ":" + from_testdata_base("recovery-from-boot.p"); - std::vector args3 = { - "applypatch", - "-b", - bonus_file.c_str(), - src_file.c_str(), - tgt_file.c_str(), - recovery_img_sha1.c_str(), - recovery_img_size.c_str(), - patch1.c_str(), - patch2.c_str(), - patch3.c_str() - }; - ASSERT_EQ(0, applypatch_modes(args3.size(), args3.data())); + std::vector args = { "applypatch", + "-b", + bonus_file.c_str(), + src_file_arg.c_str(), + tgt_file_arg.c_str(), + recovery_img_sha1.c_str(), + recovery_img_size_arg.c_str(), + patch1.c_str(), + patch2.c_str(), + patch3.c_str() }; + ASSERT_EQ(0, applypatch_modes(args.size(), args.data())); } // Ensures that applypatch works with a bsdiff based recovery-from-boot.p. From b6e6ee758704a7044bb6cdf7a17f9e1d1f7ac195 Mon Sep 17 00:00:00 2001 From: Jaegeuk Kim Date: Fri, 8 Dec 2017 13:19:23 -0800 Subject: [PATCH 008/560] f2fs: support f2fs by setting unmovable bit for package file This enables to use uncrypt for f2fs update-on-reboot. It requires kernel patch named: "f2fs: add an ioctl to disable GC for specific file" If any operation fails during uncrypt, please delete package file as soon as possible, and create the file again to move forward. IOWs, don't leave the package file for a long time. Bug: 70309376 Bug: 30170612 Change-Id: I3b4233e7da756f107be35364521699deaf2e7139 Signed-off-by: Jaegeuk Kim --- uncrypt/uncrypt.cpp | 42 ++++++++++++++++++++++++++++++++++++++---- 1 file changed, 38 insertions(+), 4 deletions(-) diff --git a/uncrypt/uncrypt.cpp b/uncrypt/uncrypt.cpp index 645faadbf4..16036f9ce0 100644 --- a/uncrypt/uncrypt.cpp +++ b/uncrypt/uncrypt.cpp @@ -172,10 +172,15 @@ static struct fstab* read_fstab() { return fstab; } -static const char* find_block_device(const char* path, bool* encryptable, bool* encrypted) { +static const char* find_block_device(const char* path, bool* encryptable, + bool* encrypted, bool* f2fs_fs) { // Look for a volume whose mount point is the prefix of path and // return its block device. Set encrypted if it's currently // encrypted. + + // ensure f2fs_fs is set to false first. + *f2fs_fs = false; + for (int i = 0; i < fstab->num_entries; ++i) { struct fstab_rec* v = &fstab->recs[i]; if (!v->mount_point) { @@ -192,6 +197,9 @@ static const char* find_block_device(const char* path, bool* encryptable, bool* *encrypted = true; } } + if (strcmp(v->fs_type, "f2fs") == 0) { + *f2fs_fs = true; + } return v->blk_device; } } @@ -244,7 +252,7 @@ static int retry_fibmap(const int fd, const char* name, int* block, const int he } static int produce_block_map(const char* path, const char* map_file, const char* blk_dev, - bool encrypted, int socket) { + bool encrypted, bool f2fs_fs, int socket) { std::string err; if (!android::base::RemoveFileIfExists(map_file, &err)) { LOG(ERROR) << "failed to remove the existing map file " << map_file << ": " << err; @@ -307,6 +315,31 @@ static int produce_block_map(const char* path, const char* map_file, const char* } } +// F2FS-specific ioctl +// It requires the below kernel commit merged in v4.16-rc1. +// 1ad71a27124c ("f2fs: add an ioctl to disable GC for specific file") +// In android-4.4, +// 56ee1e817908 ("f2fs: updates on v4.16-rc1") +// In android-4.9, +// 2f17e34672a8 ("f2fs: updates on v4.16-rc1") +// In android-4.14, +// ce767d9a55bc ("f2fs: updates on v4.16-rc1") +#ifndef F2FS_IOC_SET_PIN_FILE +#ifndef F2FS_IOCTL_MAGIC +#define F2FS_IOCTL_MAGIC 0xf5 +#endif +#define F2FS_IOC_SET_PIN_FILE _IOW(F2FS_IOCTL_MAGIC, 13, __u32) +#define F2FS_IOC_GET_PIN_FILE _IOW(F2FS_IOCTL_MAGIC, 14, __u32) +#endif + if (f2fs_fs) { + int error = ioctl(fd, F2FS_IOC_SET_PIN_FILE); + // Don't break the old kernels which don't support it. + if (error && errno != ENOTTY && errno != ENOTSUP) { + PLOG(ERROR) << "Failed to set pin_file for f2fs: " << path << " on " << blk_dev; + return kUncryptIoctlError; + } + } + off64_t pos = 0; int last_progress = 0; while (pos < sb.st_size) { @@ -458,7 +491,8 @@ static int uncrypt(const char* input_path, const char* map_file, const int socke bool encryptable; bool encrypted; - const char* blk_dev = find_block_device(path, &encryptable, &encrypted); + bool f2fs_fs; + const char* blk_dev = find_block_device(path, &encryptable, &encrypted, &f2fs_fs); if (blk_dev == nullptr) { LOG(ERROR) << "failed to find block device for " << path; return kUncryptBlockDeviceFindError; @@ -479,7 +513,7 @@ static int uncrypt(const char* input_path, const char* map_file, const int socke // and /sdcard we leave the file alone. if (strncmp(path, "/data/", 6) == 0) { LOG(INFO) << "writing block map " << map_file; - return produce_block_map(path, map_file, blk_dev, encrypted, socket); + return produce_block_map(path, map_file, blk_dev, encrypted, f2fs_fs, socket); } return 0; From ec2e8c6c1ef3cbafa129ade95abca3203e062b5f Mon Sep 17 00:00:00 2001 From: Tao Bao Date: Thu, 22 Mar 2018 16:07:00 -0700 Subject: [PATCH 009/560] update_verifier: Support verifying product partition. We have added the support for building /product partition in build system (the CL in [1]), where /product is an optional partition that contains system files. This CL adds the matching support if /product needs to be verified during A/B OTA (i.e. listed in care_map file). [1]: commit b7735d81054002961b681f4bdf296d4de2701135, https://android-review.googlesource.com/c/platform/build/+/598454 Bug: 63974895 Test: Run update_verifier test on walleye. Change-Id: Ia1c35e9583b8e66c98a4495b1f81a5ea7e65036f --- tests/component/update_verifier_test.cpp | 3 +-- update_verifier/update_verifier.cpp | 22 ++++++++++------------ 2 files changed, 11 insertions(+), 14 deletions(-) diff --git a/tests/component/update_verifier_test.cpp b/tests/component/update_verifier_test.cpp index b04e1185e7..1544bb2a46 100644 --- a/tests/component/update_verifier_test.cpp +++ b/tests/component/update_verifier_test.cpp @@ -46,7 +46,6 @@ TEST_F(UpdateVerifierTest, verify_image_smoke) { return; } - // The care map file can have only two or four lines. TemporaryFile temp_file; std::string content = "system\n2,0,1"; ASSERT_TRUE(android::base::WriteStringToFile(content, temp_file.path)); @@ -58,7 +57,7 @@ TEST_F(UpdateVerifierTest, verify_image_smoke) { } TEST_F(UpdateVerifierTest, verify_image_wrong_lines) { - // The care map file can have only two or four lines. + // The care map file can have only 2 / 4 / 6 lines. TemporaryFile temp_file; ASSERT_FALSE(verify_image(temp_file.path)); diff --git a/update_verifier/update_verifier.cpp b/update_verifier/update_verifier.cpp index c5e154f03e..92d9313710 100644 --- a/update_verifier/update_verifier.cpp +++ b/update_verifier/update_verifier.cpp @@ -74,14 +74,13 @@ static int dm_name_filter(const dirent* de) { } static bool read_blocks(const std::string& partition, const std::string& range_str) { - if (partition != "system" && partition != "vendor") { - LOG(ERROR) << "partition name must be system or vendor: " << partition; + if (partition != "system" && partition != "vendor" && partition != "product") { + LOG(ERROR) << "Invalid partition name \"" << partition << "\""; return false; } - // Iterate the content of "/sys/block/dm-X/dm/name". If it matches "system" - // (or "vendor"), then dm-X is a dm-wrapped system/vendor partition. - // Afterwards, update_verifier will read every block on the care_map_file of - // "/dev/block/dm-X" to ensure the partition's integrity. + // Iterate the content of "/sys/block/dm-X/dm/name". If it matches one of "system", "vendor" or + // "product", then dm-X is a dm-wrapped device for that target. We will later read all the + // ("cared") blocks from "/dev/block/dm-X" to ensure the target partition's integrity. static constexpr auto DM_PATH_PREFIX = "/sys/block/"; dirent** namelist; int n = scandir(DM_PATH_PREFIX, &namelist, dm_name_filter, alphasort); @@ -206,10 +205,9 @@ bool verify_image(const std::string& care_map_name) { PLOG(WARNING) << "Failed to open " << care_map_name; return true; } - // Care map file has four lines (two lines if vendor partition is not present): - // First line has the block partition name (system/vendor). - // Second line holds all ranges of blocks to verify. - // The next two lines have the same format but for vendor partition. + // care_map file has up to six lines, where every two lines make a pair. Within each pair, the + // first line has the partition name (e.g. "system"), while the second line holds the ranges of + // all the blocks to verify. std::string file_content; if (!android::base::ReadFdToString(care_map_fd.get(), &file_content)) { LOG(ERROR) << "Error reading care map contents to string."; @@ -218,9 +216,9 @@ bool verify_image(const std::string& care_map_name) { std::vector lines; lines = android::base::Split(android::base::Trim(file_content), "\n"); - if (lines.size() != 2 && lines.size() != 4) { + if (lines.size() != 2 && lines.size() != 4 && lines.size() != 6) { LOG(ERROR) << "Invalid lines in care_map: found " << lines.size() - << " lines, expecting 2 or 4 lines."; + << " lines, expecting 2 or 4 or 6 lines."; return false; } From 2b3f80068ece1040ba7c923afe1e70b705535ad5 Mon Sep 17 00:00:00 2001 From: Tianjie Xu Date: Tue, 20 Mar 2018 16:07:39 -0700 Subject: [PATCH 010/560] Factor out a menu class for screen ui Also consolidate the duplicate codes to draw the menu in ScreenRecoveryUI and WearRecoveryUI. This helps us to support text icons as menu in the future. Bug: 74397117 Test: Check the menu under recovery on bullhead and a wear device. Change-Id: Iba9b646c3828670f0e78a7e07d1a94a44e96bb0b (cherry picked from commit Iba9b646c3828670f0e78a7e07d1a94a44e96bb0b) --- Android.mk | 77 ++++++++----- screen_ui.cpp | 183 ++++++++++++++++++++++++------- screen_ui.h | 73 ++++++++++++- tests/Android.mk | 4 +- tests/unit/screen_ui_test.cpp | 198 ++++++++++++++++++++++++++++++++++ wear_ui.cpp | 116 ++------------------ wear_ui.h | 4 - 7 files changed, 471 insertions(+), 184 deletions(-) create mode 100644 tests/unit/screen_ui_test.cpp diff --git a/Android.mk b/Android.mk index 56d69c03bb..0499a6da8a 100644 --- a/Android.mk +++ b/Android.mk @@ -53,36 +53,21 @@ LOCAL_STATIC_LIBRARIES := \ include $(BUILD_STATIC_LIBRARY) -# recovery (static executable) +# librecovery_ui (static library) # =============================== include $(CLEAR_VARS) - LOCAL_SRC_FILES := \ - adb_install.cpp \ - device.cpp \ - fuse_sdcard_provider.cpp \ - recovery.cpp \ - roots.cpp \ - rotate_logs.cpp \ screen_ui.cpp \ ui.cpp \ vr_ui.cpp \ - wear_ui.cpp \ + wear_ui.cpp -LOCAL_MODULE := recovery - -LOCAL_FORCE_STATIC_EXECUTABLE := true - -LOCAL_REQUIRED_MODULES := e2fsdroid_static mke2fs_static mke2fs.conf - -ifeq ($(TARGET_USERIMAGES_USE_F2FS),true) -ifeq ($(HOST_OS),linux) -LOCAL_REQUIRED_MODULES += sload.f2fs mkfs.f2fs -endif -endif +LOCAL_CFLAGS := -Wall -Werror -LOCAL_CFLAGS += -DRECOVERY_API_VERSION=$(RECOVERY_API_VERSION) -LOCAL_CFLAGS += -Wall -Werror +LOCAL_MODULE := librecovery_ui +LOCAL_STATIC_LIBRARIES := \ + libminui \ + libbase ifneq ($(TARGET_RECOVERY_UI_MARGIN_HEIGHT),) LOCAL_CFLAGS += -DRECOVERY_UI_MARGIN_HEIGHT=$(TARGET_RECOVERY_UI_MARGIN_HEIGHT) @@ -132,6 +117,36 @@ else LOCAL_CFLAGS += -DRECOVERY_UI_VR_STEREO_OFFSET=0 endif +include $(BUILD_STATIC_LIBRARY) + +# recovery (static executable) +# =============================== +include $(CLEAR_VARS) + +LOCAL_SRC_FILES := \ + adb_install.cpp \ + device.cpp \ + fuse_sdcard_provider.cpp \ + recovery.cpp \ + roots.cpp \ + rotate_logs.cpp \ + + +LOCAL_MODULE := recovery + +LOCAL_FORCE_STATIC_EXECUTABLE := true + +LOCAL_REQUIRED_MODULES := e2fsdroid_static mke2fs_static mke2fs.conf + +ifeq ($(TARGET_USERIMAGES_USE_F2FS),true) +ifeq ($(HOST_OS),linux) +LOCAL_REQUIRED_MODULES += sload.f2fs mkfs.f2fs +endif +endif + +LOCAL_CFLAGS += -DRECOVERY_API_VERSION=$(RECOVERY_API_VERSION) +LOCAL_CFLAGS += -Wall -Werror + LOCAL_C_INCLUDES += \ system/vold \ @@ -148,8 +163,17 @@ LOCAL_STATIC_LIBRARIES := \ libvndksupport \ libbatterymonitor +LOCAL_STATIC_LIBRARIES += librecovery + +# If $(TARGET_RECOVERY_UI_LIB) is defined, the recovery calls make_device() from the +# $(TARGET_RECOVERY_UI_LIB), which depends on the librecovery_ui. +ifeq ($(TARGET_RECOVERY_UI_LIB),) + LOCAL_SRC_FILES += default_device.cpp +else + LOCAL_STATIC_LIBRARIES += $(TARGET_RECOVERY_UI_LIB) +endif + LOCAL_STATIC_LIBRARIES += \ - librecovery \ libverifier \ libbootloader_message \ libfs_mgr \ @@ -161,6 +185,7 @@ LOCAL_STATIC_LIBRARIES += \ libminadbd \ libasyncio \ libfusesideload \ + librecovery_ui \ libminui \ libpng \ libcrypto_utils \ @@ -184,12 +209,6 @@ endif LOCAL_MODULE_PATH := $(TARGET_RECOVERY_ROOT_OUT)/sbin -ifeq ($(TARGET_RECOVERY_UI_LIB),) - LOCAL_SRC_FILES += default_device.cpp -else - LOCAL_STATIC_LIBRARIES += $(TARGET_RECOVERY_UI_LIB) -endif - ifeq ($(BOARD_CACHEIMAGE_PARTITION_SIZE),) LOCAL_REQUIRED_MODULES += recovery-persist recovery-refresh endif diff --git a/screen_ui.cpp b/screen_ui.cpp index c8fb5aa755..317e5529ce 100644 --- a/screen_ui.cpp +++ b/screen_ui.cpp @@ -53,7 +53,98 @@ static double now() { return tv.tv_sec + tv.tv_usec / 1000000.0; } -ScreenRecoveryUI::ScreenRecoveryUI() +Menu::Menu(bool scrollable, size_t max_items, size_t max_length) + : scrollable_(scrollable), + max_display_items_(max_items), + max_item_length_(max_length), + text_headers_(nullptr), + menu_start_(0), + selection_(0) { + CHECK_LE(max_items, static_cast(std::numeric_limits::max())); +} + +const char* const* Menu::text_headers() const { + return text_headers_; +} + +std::string Menu::TextItem(size_t index) const { + CHECK_LT(index, text_items_.size()); + + return text_items_[index]; +} + +size_t Menu::MenuStart() const { + return menu_start_; +} + +size_t Menu::MenuEnd() const { + return std::min(ItemsCount(), menu_start_ + max_display_items_); +} + +size_t Menu::ItemsCount() const { + return text_items_.size(); +} + +bool Menu::ItemsOverflow(std::string* cur_selection_str) const { + if (!scrollable_ || static_cast(ItemsCount()) <= max_display_items_) { + return false; + } + + *cur_selection_str = + android::base::StringPrintf("Current item: %d/%zu", selection_ + 1, ItemsCount()); + return true; +} + +void Menu::Start(const char* const* headers, const char* const* items, int initial_selection) { + text_headers_ = headers; + + // It's fine to have more entries than text_rows_ if scrollable menu is supported. + size_t max_items_count = scrollable_ ? std::numeric_limits::max() : max_display_items_; + for (size_t i = 0; i < max_items_count && items[i] != nullptr; ++i) { + text_items_.emplace_back(items[i], strnlen(items[i], max_item_length_)); + } + + CHECK(!text_items_.empty()); + selection_ = initial_selection; +} + +// TODO(xunchang) modify the function parameters to button up & down. +int Menu::Select(int sel) { + CHECK_LE(ItemsCount(), static_cast(std::numeric_limits::max())); + int count = ItemsCount(); + + // Wraps the selection at boundary if the menu is not scrollable. + if (!scrollable_) { + if (sel < 0) { + selection_ = count - 1; + } else if (sel >= count) { + selection_ = 0; + } else { + selection_ = sel; + } + + return selection_; + } + + if (sel < 0) { + selection_ = 0; + } else if (sel >= count) { + selection_ = count - 1; + } else { + if (static_cast(sel) < menu_start_) { + menu_start_--; + } else if (static_cast(sel) >= MenuEnd()) { + menu_start_++; + } + selection_ = sel; + } + + return selection_; +} + +ScreenRecoveryUI::ScreenRecoveryUI() : ScreenRecoveryUI(false) {} + +ScreenRecoveryUI::ScreenRecoveryUI(bool scrollable_menu) : kMarginWidth(RECOVERY_UI_MARGIN_WIDTH), kMarginHeight(RECOVERY_UI_MARGIN_HEIGHT), kAnimationFps(RECOVERY_UI_ANIMATION_FPS), @@ -71,10 +162,7 @@ ScreenRecoveryUI::ScreenRecoveryUI() text_row_(0), show_text(false), show_text_ever(false), - menu_headers_(nullptr), - show_menu(false), - menu_items(0), - menu_sel(0), + scrollable_menu_(scrollable_menu), file_viewer_text_(nullptr), intro_frames(0), loop_frames(0), @@ -407,13 +495,13 @@ int ScreenRecoveryUI::DrawWrappedTextLines(int x, int y, const char* const* line static const char* REGULAR_HELP[] = { "Use volume up/down and power.", - NULL + nullptr, }; static const char* LONG_PRESS_HELP[] = { "Any button cycles highlight.", "Long-press activates.", - NULL + nullptr, }; // Redraws everything on the screen. Does not flip pages. Should only be called with updateMutex @@ -428,8 +516,13 @@ void ScreenRecoveryUI::draw_screen_locked() { gr_color(0, 0, 0, 255); gr_clear(); + draw_menu_and_text_buffer_locked(HasThreeButtons() ? REGULAR_HELP : LONG_PRESS_HELP); +} + +// Draws the menu and text buffer on the screen. Should only be called with updateMutex locked. +void ScreenRecoveryUI::draw_menu_and_text_buffer_locked(const char* const* help_message) { int y = kMarginHeight; - if (show_menu) { + if (menu_) { static constexpr int kMenuIndent = 4; int x = kMarginWidth + kMenuIndent; @@ -440,26 +533,46 @@ void ScreenRecoveryUI::draw_screen_locked() { for (const auto& chunk : android::base::Split(recovery_fingerprint, ":")) { y += DrawTextLine(x, y, chunk.c_str(), false); } - y += DrawTextLines(x, y, HasThreeButtons() ? REGULAR_HELP : LONG_PRESS_HELP); + y += DrawTextLines(x, y, help_message); + + // Draw menu header. SetColor(HEADER); - // Ignore kMenuIndent, which is not taken into account by text_cols_. - y += DrawWrappedTextLines(kMarginWidth, y, menu_headers_); + if (!menu_->scrollable()) { + y += DrawWrappedTextLines(x, y, menu_->text_headers()); + } else { + y += DrawTextLines(x, y, menu_->text_headers()); + // Show the current menu item number in relation to total number if items don't fit on the + // screen. + std::string cur_selection_str; + if (menu_->ItemsOverflow(&cur_selection_str)) { + y += DrawTextLine(x, y, cur_selection_str.c_str(), true); + } + } + // Draw menu items. SetColor(MENU); - y += DrawHorizontalRule(y) + 4; - for (int i = 0; i < menu_items; ++i) { - if (i == menu_sel) { + // Do not draw the horizontal rule for wear devices. + if (!menu_->scrollable()) { + y += DrawHorizontalRule(y) + 4; + } + for (size_t i = menu_->MenuStart(); i < menu_->MenuEnd(); ++i) { + bool bold = false; + if (i == static_cast(menu_->selection())) { // Draw the highlight bar. SetColor(IsLongPress() ? MENU_SEL_BG_ACTIVE : MENU_SEL_BG); - DrawHighlightBar(0, y - 2, ScreenWidth(), char_height_ + 4); + + int bar_height = char_height_ + 4; + DrawHighlightBar(0, y - 2, ScreenWidth(), bar_height); + // Bold white text for the selected item. SetColor(MENU_SEL_FG); - y += DrawTextLine(x, y, menu_[i].c_str(), true); - SetColor(MENU); - } else { - y += DrawTextLine(x, y, menu_[i].c_str(), false); + bold = true; } + + y += DrawTextLine(x, y, menu_->TextItem(i).c_str(), bold); + + SetColor(MENU); } y += DrawHorizontalRule(y); } @@ -864,15 +977,10 @@ void ScreenRecoveryUI::ShowFile(const char* filename) { void ScreenRecoveryUI::StartMenu(const char* const* headers, const char* const* items, int initial_selection) { pthread_mutex_lock(&updateMutex); - if (text_rows_ > 0 && text_cols_ > 0) { - menu_headers_ = headers; - menu_.clear(); - for (size_t i = 0; i < text_rows_ && items[i] != nullptr; ++i) { - menu_.emplace_back(std::string(items[i], strnlen(items[i], text_cols_ - 1))); - } - menu_items = static_cast(menu_.size()); - show_menu = true; - menu_sel = initial_selection; + if (text_rows_ > 0 && text_cols_ > 1) { + menu_ = std::make_unique(scrollable_menu_, text_rows_, text_cols_ - 1); + menu_->Start(headers, items, initial_selection); + update_screen_locked(); } pthread_mutex_unlock(&updateMutex); @@ -880,16 +988,13 @@ void ScreenRecoveryUI::StartMenu(const char* const* headers, const char* const* int ScreenRecoveryUI::SelectMenu(int sel) { pthread_mutex_lock(&updateMutex); - if (show_menu) { - int old_sel = menu_sel; - menu_sel = sel; - - // Wrap at top and bottom. - if (menu_sel < 0) menu_sel = menu_items - 1; - if (menu_sel >= menu_items) menu_sel = 0; + if (menu_) { + int old_sel = menu_->selection(); + sel = menu_->Select(sel); - sel = menu_sel; - if (menu_sel != old_sel) update_screen_locked(); + if (sel != old_sel) { + update_screen_locked(); + } } pthread_mutex_unlock(&updateMutex); return sel; @@ -897,8 +1002,8 @@ int ScreenRecoveryUI::SelectMenu(int sel) { void ScreenRecoveryUI::EndMenu() { pthread_mutex_lock(&updateMutex); - if (show_menu && text_rows_ > 0 && text_cols_ > 0) { - show_menu = false; + if (menu_) { + menu_.reset(); update_screen_locked(); } pthread_mutex_unlock(&updateMutex); diff --git a/screen_ui.h b/screen_ui.h index f05761c42b..c1222a5768 100644 --- a/screen_ui.h +++ b/screen_ui.h @@ -20,6 +20,7 @@ #include #include +#include #include #include @@ -28,6 +29,70 @@ // From minui/minui.h. struct GRSurface; +// This class maintains the menu selection and display of the screen ui. +class Menu { + public: + Menu(bool scrollable, size_t max_items, size_t max_length); + + bool scrollable() const { + return scrollable_; + } + + int selection() const { + return selection_; + } + + // Returns count of menu items. + size_t ItemsCount() const; + // Returns the index of the first menu item. + size_t MenuStart() const; + // Returns the index of the last menu item + 1. + size_t MenuEnd() const; + + // Menu example: + // info: Android Recovery + // .... + // help messages: Swipe up/down to move + // Swipe left/right to select + // empty line (horizontal rule): + // menu headers: Select file to view + // menu items: /cache/recovery/last_log + // /cache/recovery/last_log.1 + // /cache/recovery/last_log.2 + // ... + const char* const* text_headers() const; + std::string TextItem(size_t index) const; + + // Checks if the menu items fit vertically on the screen. Returns true and set the + // |cur_selection_str| if the items exceed the screen limit. + bool ItemsOverflow(std::string* cur_selection_str) const; + + // Starts the menu with |headers| and |items| in text. Sets the default selection to + // |initial_selection|. + void Start(const char* const* headers, const char* const* items, int initial_selection); + + // Sets the current selection to |sel|. Handle the overflow cases depending on if the menu is + // scrollable. + int Select(int sel); + + private: + // The menu is scrollable to display more items. Used on wear devices who have smaller screens. + const bool scrollable_; + // The max number of menu items to fit vertically on a screen. + const size_t max_display_items_; + // The length of each item to fit horizontally on a screen. + const size_t max_item_length_; + + // Internal storage for the menu headers and items in text. + const char* const* text_headers_; + std::vector text_items_; + + // The first item to display on the screen. + size_t menu_start_; + // Current menu selection. + int selection_; +}; + // Implementation of RecoveryUI appropriate for devices with a screen // (shows an icon + a progress bar, text logging, menu, etc.) class ScreenRecoveryUI : public RecoveryUI { @@ -44,6 +109,7 @@ class ScreenRecoveryUI : public RecoveryUI { }; ScreenRecoveryUI(); + explicit ScreenRecoveryUI(bool scrollable_menu); bool Init(const std::string& locale) override; @@ -101,6 +167,7 @@ class ScreenRecoveryUI : public RecoveryUI { virtual void draw_background_locked(); virtual void draw_foreground_locked(); virtual void draw_screen_locked(); + virtual void draw_menu_and_text_buffer_locked(const char* const* help_message); virtual void update_screen_locked(); virtual void update_progress_locked(); @@ -184,10 +251,8 @@ class ScreenRecoveryUI : public RecoveryUI { bool show_text; bool show_text_ever; // has show_text ever been true? - std::vector menu_; - const char* const* menu_headers_; - bool show_menu; - int menu_items, menu_sel; + bool scrollable_menu_; + std::unique_ptr menu_; // An alternate text screen, swapped with 'text_' when we're viewing a log file. char** file_viewer_text_; diff --git a/tests/Android.mk b/tests/Android.mk index b3584fe87a..9a71371faf 100644 --- a/tests/Android.mk +++ b/tests/Android.mk @@ -23,6 +23,7 @@ LOCAL_MODULE := recovery_unit_test LOCAL_COMPATIBILITY_SUITE := device-tests LOCAL_STATIC_LIBRARIES := \ libverifier \ + librecovery_ui \ libminui \ libotautil \ libupdater \ @@ -38,8 +39,9 @@ LOCAL_SRC_FILES := \ unit/dirutil_test.cpp \ unit/locale_test.cpp \ unit/rangeset_test.cpp \ + unit/screen_ui_test.cpp \ unit/sysutil_test.cpp \ - unit/zip_test.cpp \ + unit/zip_test.cpp LOCAL_C_INCLUDES := bootable/recovery LOCAL_SHARED_LIBRARIES := liblog diff --git a/tests/unit/screen_ui_test.cpp b/tests/unit/screen_ui_test.cpp new file mode 100644 index 0000000000..be6799f2e0 --- /dev/null +++ b/tests/unit/screen_ui_test.cpp @@ -0,0 +1,198 @@ +/* + * Copyright (C) 2018 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 "screen_ui.h" + +#include + +#include + +constexpr const char* HEADER[] = { "header", nullptr }; +constexpr const char* ITEMS[] = { "items1", "items2", "items3", "items4", "1234567890", nullptr }; + +TEST(ScreenUITest, StartPhoneMenuSmoke) { + Menu menu(false, 10, 20); + ASSERT_FALSE(menu.scrollable()); + + menu.Start(HEADER, ITEMS, 0); + ASSERT_EQ(HEADER[0], menu.text_headers()[0]); + ASSERT_EQ(5u, menu.ItemsCount()); + + std::string message; + ASSERT_FALSE(menu.ItemsOverflow(&message)); + for (size_t i = 0; i < menu.ItemsCount(); i++) { + ASSERT_EQ(ITEMS[i], menu.TextItem(i)); + } + + ASSERT_EQ(0, menu.selection()); +} + +TEST(ScreenUITest, StartWearMenuSmoke) { + Menu menu(true, 10, 8); + ASSERT_TRUE(menu.scrollable()); + + menu.Start(HEADER, ITEMS, 1); + ASSERT_EQ(HEADER[0], menu.text_headers()[0]); + ASSERT_EQ(5u, menu.ItemsCount()); + + std::string message; + ASSERT_FALSE(menu.ItemsOverflow(&message)); + for (size_t i = 0; i < menu.ItemsCount() - 1; i++) { + ASSERT_EQ(ITEMS[i], menu.TextItem(i)); + } + // Test of the last item is truncated + ASSERT_EQ("12345678", menu.TextItem(4)); + ASSERT_EQ(1, menu.selection()); +} + +TEST(ScreenUITest, StartPhoneMenuItemsOverflow) { + Menu menu(false, 1, 20); + ASSERT_FALSE(menu.scrollable()); + + menu.Start(HEADER, ITEMS, 0); + ASSERT_EQ(1u, menu.ItemsCount()); + + std::string message; + ASSERT_FALSE(menu.ItemsOverflow(&message)); + for (size_t i = 0; i < menu.ItemsCount(); i++) { + ASSERT_EQ(ITEMS[i], menu.TextItem(i)); + } + + ASSERT_EQ(0u, menu.MenuStart()); + ASSERT_EQ(1u, menu.MenuEnd()); +} + +TEST(ScreenUITest, StartWearMenuItemsOverflow) { + Menu menu(true, 1, 20); + ASSERT_TRUE(menu.scrollable()); + + menu.Start(HEADER, ITEMS, 0); + ASSERT_EQ(5u, menu.ItemsCount()); + + std::string message; + ASSERT_TRUE(menu.ItemsOverflow(&message)); + ASSERT_EQ("Current item: 1/5", message); + + for (size_t i = 0; i < menu.ItemsCount(); i++) { + ASSERT_EQ(ITEMS[i], menu.TextItem(i)); + } + + ASSERT_EQ(0u, menu.MenuStart()); + ASSERT_EQ(1u, menu.MenuEnd()); +} + +TEST(ScreenUITest, PhoneMenuSelectSmoke) { + Menu menu(false, 10, 20); + + int sel = 0; + menu.Start(HEADER, ITEMS, sel); + // Mimic down button 10 times (2 * items size) + for (int i = 0; i < 10; i++) { + sel = menu.Select(++sel); + ASSERT_EQ(sel, menu.selection()); + + // Wraps the selection for unscrollable menu when it reaches the boundary. + int expected = (i + 1) % 5; + ASSERT_EQ(expected, menu.selection()); + + ASSERT_EQ(0u, menu.MenuStart()); + ASSERT_EQ(5u, menu.MenuEnd()); + } + + // Mimic up button 10 times + for (int i = 0; i < 10; i++) { + sel = menu.Select(--sel); + ASSERT_EQ(sel, menu.selection()); + + int expected = (9 - i) % 5; + ASSERT_EQ(expected, menu.selection()); + + ASSERT_EQ(0u, menu.MenuStart()); + ASSERT_EQ(5u, menu.MenuEnd()); + } +} + +TEST(ScreenUITest, WearMenuSelectSmoke) { + Menu menu(true, 10, 20); + + int sel = 0; + menu.Start(HEADER, ITEMS, sel); + // Mimic pressing down button 10 times (2 * items size) + for (int i = 0; i < 10; i++) { + sel = menu.Select(++sel); + ASSERT_EQ(sel, menu.selection()); + + // Stops the selection at the boundary if the menu is scrollable. + int expected = std::min(i + 1, 4); + ASSERT_EQ(expected, menu.selection()); + + ASSERT_EQ(0u, menu.MenuStart()); + ASSERT_EQ(5u, menu.MenuEnd()); + } + + // Mimic pressing up button 10 times + for (int i = 0; i < 10; i++) { + sel = menu.Select(--sel); + ASSERT_EQ(sel, menu.selection()); + + int expected = std::max(3 - i, 0); + ASSERT_EQ(expected, menu.selection()); + + ASSERT_EQ(0u, menu.MenuStart()); + ASSERT_EQ(5u, menu.MenuEnd()); + } +} + +TEST(ScreenUITest, WearMenuSelectItemsOverflow) { + Menu menu(true, 3, 20); + + int sel = 1; + menu.Start(HEADER, ITEMS, sel); + ASSERT_EQ(5u, menu.ItemsCount()); + + // Scroll the menu to the end, and check the start & end of menu. + for (int i = 0; i < 3; i++) { + sel = menu.Select(++sel); + ASSERT_EQ(i + 2, sel); + ASSERT_EQ(static_cast(i), menu.MenuStart()); + ASSERT_EQ(static_cast(i + 3), menu.MenuEnd()); + } + + // Press down button one more time won't change the MenuStart() and MenuEnd(). + sel = menu.Select(++sel); + ASSERT_EQ(4, sel); + ASSERT_EQ(2u, menu.MenuStart()); + ASSERT_EQ(5u, menu.MenuEnd()); + + // Scroll the menu to the top. + // The expected menu sel, start & ends are: + // sel 3, start 2, end 5 + // sel 2, start 2, end 5 + // sel 1, start 1, end 4 + // sel 0, start 0, end 3 + for (int i = 0; i < 4; i++) { + sel = menu.Select(--sel); + ASSERT_EQ(3 - i, sel); + ASSERT_EQ(static_cast(std::min(3 - i, 2)), menu.MenuStart()); + ASSERT_EQ(static_cast(std::min(6 - i, 5)), menu.MenuEnd()); + } + + // Press up button one more time won't change the MenuStart() and MenuEnd(). + sel = menu.Select(--sel); + ASSERT_EQ(0, sel); + ASSERT_EQ(0u, menu.MenuStart()); + ASSERT_EQ(3u, menu.MenuEnd()); +} diff --git a/wear_ui.cpp b/wear_ui.cpp index ca6b1b1028..118e435084 100644 --- a/wear_ui.cpp +++ b/wear_ui.cpp @@ -17,7 +17,6 @@ #include "wear_ui.h" #include -#include // TODO: Remove after killing the call to sprintf(). #include #include @@ -27,7 +26,8 @@ #include WearRecoveryUI::WearRecoveryUI() - : kProgressBarBaseline(RECOVERY_UI_PROGRESS_BAR_BASELINE), + : ScreenRecoveryUI(true), + kProgressBarBaseline(RECOVERY_UI_PROGRESS_BAR_BASELINE), kMenuUnusableRows(RECOVERY_UI_MENU_UNUSABLE_ROWS) { // TODO: kMenuUnusableRows should be computed based on the lines in draw_screen_locked(). @@ -65,13 +65,10 @@ static const char* SWIPE_HELP[] = { "Swipe up/down to move.", "Swipe left/right to select.", "", - NULL + nullptr, }; -// TODO merge drawing routines with screen_ui void WearRecoveryUI::draw_screen_locked() { - char cur_selection_str[50]; - draw_background_locked(); if (!show_text) { draw_foreground_locked(); @@ -79,68 +76,7 @@ void WearRecoveryUI::draw_screen_locked() { SetColor(TEXT_FILL); gr_fill(0, 0, gr_fb_width(), gr_fb_height()); - int y = kMarginHeight; - int x = kMarginWidth; - if (show_menu) { - std::string recovery_fingerprint = - android::base::GetProperty("ro.bootimage.build.fingerprint", ""); - SetColor(HEADER); - y += DrawTextLine(x + 4, y, "Android Recovery", true); - for (auto& chunk : android::base::Split(recovery_fingerprint, ":")) { - y += DrawTextLine(x + 4, y, chunk.c_str(), false); - } - - // This is actually the help strings. - y += DrawTextLines(x + 4, y, SWIPE_HELP); - SetColor(HEADER); - y += DrawTextLines(x + 4, y, menu_headers_); - - // Show the current menu item number in relation to total number if - // 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(gr_sys_font(), x + 4, y, cur_selection_str, 1); - y += char_height_ + 4; - } - - // Menu begins here - SetColor(MENU); - - for (int i = menu_start; i < menu_end; ++i) { - if (i == menu_sel) { - // draw the highlight bar - SetColor(MENU_SEL_BG); - 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(gr_sys_font(), x + 4, y, menu_[i].c_str(), 1); - } - SetColor(MENU); - } else if (menu_[i][0]) { - gr_text(gr_sys_font(), x + 4, y, menu_[i].c_str(), 0); - } - y += char_height_ + 4; - } - SetColor(MENU); - y += 4; - gr_fill(0, y, gr_fb_width(), y + 2); - y += 4; - } - - SetColor(LOG); - - // display from the bottom up, until we hit the top of the - // screen, the bottom of the menu, or we've displayed the - // entire text buffer. - int row = text_row_; - size_t count = 0; - for (int ty = gr_fb_height() - char_height_ - kMarginHeight; ty > y + 2 && count < text_rows_; - ty -= char_height_, ++count) { - gr_text(gr_sys_font(), x + 4, ty, text_[row], 0); - --row; - if (row < 0) row = text_rows_ - 1; - } + draw_menu_and_text_buffer_locked(SWIPE_HELP); } } @@ -156,45 +92,11 @@ void WearRecoveryUI::StartMenu(const char* const* headers, const char* const* it int initial_selection) { pthread_mutex_lock(&updateMutex); if (text_rows_ > 0 && text_cols_ > 0) { - menu_headers_ = headers; - menu_.clear(); - // "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. - // Bug: 23752519 - for (size_t i = 0; items[i] != nullptr; i++) { - menu_.emplace_back(std::string(items[i], strnlen(items[i], text_cols_ - 1))); - } - menu_items = static_cast(menu_.size()); - show_menu = true; - menu_sel = initial_selection; - menu_start = 0; - menu_end = text_rows_ - 1 - kMenuUnusableRows; - if (menu_items <= menu_end) menu_end = menu_items; - update_screen_locked(); - } - pthread_mutex_unlock(&updateMutex); -} + menu_ = std::make_unique(scrollable_menu_, text_rows_ - kMenuUnusableRows - 1, + text_cols_ - 1); + menu_->Start(headers, items, initial_selection); -int WearRecoveryUI::SelectMenu(int sel) { - int old_sel; - pthread_mutex_lock(&updateMutex); - if (show_menu) { - old_sel = menu_sel; - menu_sel = sel; - if (menu_sel < 0) menu_sel = 0; - if (menu_sel >= menu_items) menu_sel = menu_items - 1; - if (menu_sel < menu_start) { - menu_start--; - menu_end--; - } else if (menu_sel >= menu_end && menu_sel < menu_items) { - menu_end++; - menu_start++; - } - sel = menu_sel; - if (menu_sel != old_sel) update_screen_locked(); + update_screen_locked(); } pthread_mutex_unlock(&updateMutex); - return sel; -} +} \ No newline at end of file diff --git a/wear_ui.h b/wear_ui.h index 739b4cb1d8..8b24cb73e4 100644 --- a/wear_ui.h +++ b/wear_ui.h @@ -25,10 +25,8 @@ class WearRecoveryUI : public ScreenRecoveryUI { void SetStage(int current, int max) override; - // menu display void StartMenu(const char* const* headers, const char* const* items, int initial_selection) override; - int SelectMenu(int sel) override; protected: // progress bar vertical position, it's centered horizontally @@ -45,8 +43,6 @@ class WearRecoveryUI : public ScreenRecoveryUI { private: void draw_background_locked() override; void draw_screen_locked() override; - - int menu_start, menu_end; }; #endif // RECOVERY_WEAR_UI_H From ba0460c981d8178859d4987f3554401545228a71 Mon Sep 17 00:00:00 2001 From: Tianjie Xu Date: Tue, 20 Mar 2018 16:07:39 -0700 Subject: [PATCH 011/560] Factor out a menu class for screen ui Also consolidate the duplicate codes to draw the menu in ScreenRecoveryUI and WearRecoveryUI. This helps us to support text icons as menu in the future. Bug: 74397117 Test: Check the menu under recovery on bullhead and a wear device. Change-Id: Iba9b646c3828670f0e78a7e07d1a94a44e96bb0b (cherry picked from commit 2b3f80068ece1040ba7c923afe1e70b705535ad5) --- Android.mk | 77 ++++++++----- screen_ui.cpp | 183 ++++++++++++++++++++++++------- screen_ui.h | 73 ++++++++++++- tests/Android.mk | 4 +- tests/unit/screen_ui_test.cpp | 198 ++++++++++++++++++++++++++++++++++ wear_ui.cpp | 116 ++------------------ wear_ui.h | 4 - 7 files changed, 471 insertions(+), 184 deletions(-) create mode 100644 tests/unit/screen_ui_test.cpp diff --git a/Android.mk b/Android.mk index 56d69c03bb..0499a6da8a 100644 --- a/Android.mk +++ b/Android.mk @@ -53,36 +53,21 @@ LOCAL_STATIC_LIBRARIES := \ include $(BUILD_STATIC_LIBRARY) -# recovery (static executable) +# librecovery_ui (static library) # =============================== include $(CLEAR_VARS) - LOCAL_SRC_FILES := \ - adb_install.cpp \ - device.cpp \ - fuse_sdcard_provider.cpp \ - recovery.cpp \ - roots.cpp \ - rotate_logs.cpp \ screen_ui.cpp \ ui.cpp \ vr_ui.cpp \ - wear_ui.cpp \ + wear_ui.cpp -LOCAL_MODULE := recovery - -LOCAL_FORCE_STATIC_EXECUTABLE := true - -LOCAL_REQUIRED_MODULES := e2fsdroid_static mke2fs_static mke2fs.conf - -ifeq ($(TARGET_USERIMAGES_USE_F2FS),true) -ifeq ($(HOST_OS),linux) -LOCAL_REQUIRED_MODULES += sload.f2fs mkfs.f2fs -endif -endif +LOCAL_CFLAGS := -Wall -Werror -LOCAL_CFLAGS += -DRECOVERY_API_VERSION=$(RECOVERY_API_VERSION) -LOCAL_CFLAGS += -Wall -Werror +LOCAL_MODULE := librecovery_ui +LOCAL_STATIC_LIBRARIES := \ + libminui \ + libbase ifneq ($(TARGET_RECOVERY_UI_MARGIN_HEIGHT),) LOCAL_CFLAGS += -DRECOVERY_UI_MARGIN_HEIGHT=$(TARGET_RECOVERY_UI_MARGIN_HEIGHT) @@ -132,6 +117,36 @@ else LOCAL_CFLAGS += -DRECOVERY_UI_VR_STEREO_OFFSET=0 endif +include $(BUILD_STATIC_LIBRARY) + +# recovery (static executable) +# =============================== +include $(CLEAR_VARS) + +LOCAL_SRC_FILES := \ + adb_install.cpp \ + device.cpp \ + fuse_sdcard_provider.cpp \ + recovery.cpp \ + roots.cpp \ + rotate_logs.cpp \ + + +LOCAL_MODULE := recovery + +LOCAL_FORCE_STATIC_EXECUTABLE := true + +LOCAL_REQUIRED_MODULES := e2fsdroid_static mke2fs_static mke2fs.conf + +ifeq ($(TARGET_USERIMAGES_USE_F2FS),true) +ifeq ($(HOST_OS),linux) +LOCAL_REQUIRED_MODULES += sload.f2fs mkfs.f2fs +endif +endif + +LOCAL_CFLAGS += -DRECOVERY_API_VERSION=$(RECOVERY_API_VERSION) +LOCAL_CFLAGS += -Wall -Werror + LOCAL_C_INCLUDES += \ system/vold \ @@ -148,8 +163,17 @@ LOCAL_STATIC_LIBRARIES := \ libvndksupport \ libbatterymonitor +LOCAL_STATIC_LIBRARIES += librecovery + +# If $(TARGET_RECOVERY_UI_LIB) is defined, the recovery calls make_device() from the +# $(TARGET_RECOVERY_UI_LIB), which depends on the librecovery_ui. +ifeq ($(TARGET_RECOVERY_UI_LIB),) + LOCAL_SRC_FILES += default_device.cpp +else + LOCAL_STATIC_LIBRARIES += $(TARGET_RECOVERY_UI_LIB) +endif + LOCAL_STATIC_LIBRARIES += \ - librecovery \ libverifier \ libbootloader_message \ libfs_mgr \ @@ -161,6 +185,7 @@ LOCAL_STATIC_LIBRARIES += \ libminadbd \ libasyncio \ libfusesideload \ + librecovery_ui \ libminui \ libpng \ libcrypto_utils \ @@ -184,12 +209,6 @@ endif LOCAL_MODULE_PATH := $(TARGET_RECOVERY_ROOT_OUT)/sbin -ifeq ($(TARGET_RECOVERY_UI_LIB),) - LOCAL_SRC_FILES += default_device.cpp -else - LOCAL_STATIC_LIBRARIES += $(TARGET_RECOVERY_UI_LIB) -endif - ifeq ($(BOARD_CACHEIMAGE_PARTITION_SIZE),) LOCAL_REQUIRED_MODULES += recovery-persist recovery-refresh endif diff --git a/screen_ui.cpp b/screen_ui.cpp index c8fb5aa755..317e5529ce 100644 --- a/screen_ui.cpp +++ b/screen_ui.cpp @@ -53,7 +53,98 @@ static double now() { return tv.tv_sec + tv.tv_usec / 1000000.0; } -ScreenRecoveryUI::ScreenRecoveryUI() +Menu::Menu(bool scrollable, size_t max_items, size_t max_length) + : scrollable_(scrollable), + max_display_items_(max_items), + max_item_length_(max_length), + text_headers_(nullptr), + menu_start_(0), + selection_(0) { + CHECK_LE(max_items, static_cast(std::numeric_limits::max())); +} + +const char* const* Menu::text_headers() const { + return text_headers_; +} + +std::string Menu::TextItem(size_t index) const { + CHECK_LT(index, text_items_.size()); + + return text_items_[index]; +} + +size_t Menu::MenuStart() const { + return menu_start_; +} + +size_t Menu::MenuEnd() const { + return std::min(ItemsCount(), menu_start_ + max_display_items_); +} + +size_t Menu::ItemsCount() const { + return text_items_.size(); +} + +bool Menu::ItemsOverflow(std::string* cur_selection_str) const { + if (!scrollable_ || static_cast(ItemsCount()) <= max_display_items_) { + return false; + } + + *cur_selection_str = + android::base::StringPrintf("Current item: %d/%zu", selection_ + 1, ItemsCount()); + return true; +} + +void Menu::Start(const char* const* headers, const char* const* items, int initial_selection) { + text_headers_ = headers; + + // It's fine to have more entries than text_rows_ if scrollable menu is supported. + size_t max_items_count = scrollable_ ? std::numeric_limits::max() : max_display_items_; + for (size_t i = 0; i < max_items_count && items[i] != nullptr; ++i) { + text_items_.emplace_back(items[i], strnlen(items[i], max_item_length_)); + } + + CHECK(!text_items_.empty()); + selection_ = initial_selection; +} + +// TODO(xunchang) modify the function parameters to button up & down. +int Menu::Select(int sel) { + CHECK_LE(ItemsCount(), static_cast(std::numeric_limits::max())); + int count = ItemsCount(); + + // Wraps the selection at boundary if the menu is not scrollable. + if (!scrollable_) { + if (sel < 0) { + selection_ = count - 1; + } else if (sel >= count) { + selection_ = 0; + } else { + selection_ = sel; + } + + return selection_; + } + + if (sel < 0) { + selection_ = 0; + } else if (sel >= count) { + selection_ = count - 1; + } else { + if (static_cast(sel) < menu_start_) { + menu_start_--; + } else if (static_cast(sel) >= MenuEnd()) { + menu_start_++; + } + selection_ = sel; + } + + return selection_; +} + +ScreenRecoveryUI::ScreenRecoveryUI() : ScreenRecoveryUI(false) {} + +ScreenRecoveryUI::ScreenRecoveryUI(bool scrollable_menu) : kMarginWidth(RECOVERY_UI_MARGIN_WIDTH), kMarginHeight(RECOVERY_UI_MARGIN_HEIGHT), kAnimationFps(RECOVERY_UI_ANIMATION_FPS), @@ -71,10 +162,7 @@ ScreenRecoveryUI::ScreenRecoveryUI() text_row_(0), show_text(false), show_text_ever(false), - menu_headers_(nullptr), - show_menu(false), - menu_items(0), - menu_sel(0), + scrollable_menu_(scrollable_menu), file_viewer_text_(nullptr), intro_frames(0), loop_frames(0), @@ -407,13 +495,13 @@ int ScreenRecoveryUI::DrawWrappedTextLines(int x, int y, const char* const* line static const char* REGULAR_HELP[] = { "Use volume up/down and power.", - NULL + nullptr, }; static const char* LONG_PRESS_HELP[] = { "Any button cycles highlight.", "Long-press activates.", - NULL + nullptr, }; // Redraws everything on the screen. Does not flip pages. Should only be called with updateMutex @@ -428,8 +516,13 @@ void ScreenRecoveryUI::draw_screen_locked() { gr_color(0, 0, 0, 255); gr_clear(); + draw_menu_and_text_buffer_locked(HasThreeButtons() ? REGULAR_HELP : LONG_PRESS_HELP); +} + +// Draws the menu and text buffer on the screen. Should only be called with updateMutex locked. +void ScreenRecoveryUI::draw_menu_and_text_buffer_locked(const char* const* help_message) { int y = kMarginHeight; - if (show_menu) { + if (menu_) { static constexpr int kMenuIndent = 4; int x = kMarginWidth + kMenuIndent; @@ -440,26 +533,46 @@ void ScreenRecoveryUI::draw_screen_locked() { for (const auto& chunk : android::base::Split(recovery_fingerprint, ":")) { y += DrawTextLine(x, y, chunk.c_str(), false); } - y += DrawTextLines(x, y, HasThreeButtons() ? REGULAR_HELP : LONG_PRESS_HELP); + y += DrawTextLines(x, y, help_message); + + // Draw menu header. SetColor(HEADER); - // Ignore kMenuIndent, which is not taken into account by text_cols_. - y += DrawWrappedTextLines(kMarginWidth, y, menu_headers_); + if (!menu_->scrollable()) { + y += DrawWrappedTextLines(x, y, menu_->text_headers()); + } else { + y += DrawTextLines(x, y, menu_->text_headers()); + // Show the current menu item number in relation to total number if items don't fit on the + // screen. + std::string cur_selection_str; + if (menu_->ItemsOverflow(&cur_selection_str)) { + y += DrawTextLine(x, y, cur_selection_str.c_str(), true); + } + } + // Draw menu items. SetColor(MENU); - y += DrawHorizontalRule(y) + 4; - for (int i = 0; i < menu_items; ++i) { - if (i == menu_sel) { + // Do not draw the horizontal rule for wear devices. + if (!menu_->scrollable()) { + y += DrawHorizontalRule(y) + 4; + } + for (size_t i = menu_->MenuStart(); i < menu_->MenuEnd(); ++i) { + bool bold = false; + if (i == static_cast(menu_->selection())) { // Draw the highlight bar. SetColor(IsLongPress() ? MENU_SEL_BG_ACTIVE : MENU_SEL_BG); - DrawHighlightBar(0, y - 2, ScreenWidth(), char_height_ + 4); + + int bar_height = char_height_ + 4; + DrawHighlightBar(0, y - 2, ScreenWidth(), bar_height); + // Bold white text for the selected item. SetColor(MENU_SEL_FG); - y += DrawTextLine(x, y, menu_[i].c_str(), true); - SetColor(MENU); - } else { - y += DrawTextLine(x, y, menu_[i].c_str(), false); + bold = true; } + + y += DrawTextLine(x, y, menu_->TextItem(i).c_str(), bold); + + SetColor(MENU); } y += DrawHorizontalRule(y); } @@ -864,15 +977,10 @@ void ScreenRecoveryUI::ShowFile(const char* filename) { void ScreenRecoveryUI::StartMenu(const char* const* headers, const char* const* items, int initial_selection) { pthread_mutex_lock(&updateMutex); - if (text_rows_ > 0 && text_cols_ > 0) { - menu_headers_ = headers; - menu_.clear(); - for (size_t i = 0; i < text_rows_ && items[i] != nullptr; ++i) { - menu_.emplace_back(std::string(items[i], strnlen(items[i], text_cols_ - 1))); - } - menu_items = static_cast(menu_.size()); - show_menu = true; - menu_sel = initial_selection; + if (text_rows_ > 0 && text_cols_ > 1) { + menu_ = std::make_unique(scrollable_menu_, text_rows_, text_cols_ - 1); + menu_->Start(headers, items, initial_selection); + update_screen_locked(); } pthread_mutex_unlock(&updateMutex); @@ -880,16 +988,13 @@ void ScreenRecoveryUI::StartMenu(const char* const* headers, const char* const* int ScreenRecoveryUI::SelectMenu(int sel) { pthread_mutex_lock(&updateMutex); - if (show_menu) { - int old_sel = menu_sel; - menu_sel = sel; - - // Wrap at top and bottom. - if (menu_sel < 0) menu_sel = menu_items - 1; - if (menu_sel >= menu_items) menu_sel = 0; + if (menu_) { + int old_sel = menu_->selection(); + sel = menu_->Select(sel); - sel = menu_sel; - if (menu_sel != old_sel) update_screen_locked(); + if (sel != old_sel) { + update_screen_locked(); + } } pthread_mutex_unlock(&updateMutex); return sel; @@ -897,8 +1002,8 @@ int ScreenRecoveryUI::SelectMenu(int sel) { void ScreenRecoveryUI::EndMenu() { pthread_mutex_lock(&updateMutex); - if (show_menu && text_rows_ > 0 && text_cols_ > 0) { - show_menu = false; + if (menu_) { + menu_.reset(); update_screen_locked(); } pthread_mutex_unlock(&updateMutex); diff --git a/screen_ui.h b/screen_ui.h index f05761c42b..c1222a5768 100644 --- a/screen_ui.h +++ b/screen_ui.h @@ -20,6 +20,7 @@ #include #include +#include #include #include @@ -28,6 +29,70 @@ // From minui/minui.h. struct GRSurface; +// This class maintains the menu selection and display of the screen ui. +class Menu { + public: + Menu(bool scrollable, size_t max_items, size_t max_length); + + bool scrollable() const { + return scrollable_; + } + + int selection() const { + return selection_; + } + + // Returns count of menu items. + size_t ItemsCount() const; + // Returns the index of the first menu item. + size_t MenuStart() const; + // Returns the index of the last menu item + 1. + size_t MenuEnd() const; + + // Menu example: + // info: Android Recovery + // .... + // help messages: Swipe up/down to move + // Swipe left/right to select + // empty line (horizontal rule): + // menu headers: Select file to view + // menu items: /cache/recovery/last_log + // /cache/recovery/last_log.1 + // /cache/recovery/last_log.2 + // ... + const char* const* text_headers() const; + std::string TextItem(size_t index) const; + + // Checks if the menu items fit vertically on the screen. Returns true and set the + // |cur_selection_str| if the items exceed the screen limit. + bool ItemsOverflow(std::string* cur_selection_str) const; + + // Starts the menu with |headers| and |items| in text. Sets the default selection to + // |initial_selection|. + void Start(const char* const* headers, const char* const* items, int initial_selection); + + // Sets the current selection to |sel|. Handle the overflow cases depending on if the menu is + // scrollable. + int Select(int sel); + + private: + // The menu is scrollable to display more items. Used on wear devices who have smaller screens. + const bool scrollable_; + // The max number of menu items to fit vertically on a screen. + const size_t max_display_items_; + // The length of each item to fit horizontally on a screen. + const size_t max_item_length_; + + // Internal storage for the menu headers and items in text. + const char* const* text_headers_; + std::vector text_items_; + + // The first item to display on the screen. + size_t menu_start_; + // Current menu selection. + int selection_; +}; + // Implementation of RecoveryUI appropriate for devices with a screen // (shows an icon + a progress bar, text logging, menu, etc.) class ScreenRecoveryUI : public RecoveryUI { @@ -44,6 +109,7 @@ class ScreenRecoveryUI : public RecoveryUI { }; ScreenRecoveryUI(); + explicit ScreenRecoveryUI(bool scrollable_menu); bool Init(const std::string& locale) override; @@ -101,6 +167,7 @@ class ScreenRecoveryUI : public RecoveryUI { virtual void draw_background_locked(); virtual void draw_foreground_locked(); virtual void draw_screen_locked(); + virtual void draw_menu_and_text_buffer_locked(const char* const* help_message); virtual void update_screen_locked(); virtual void update_progress_locked(); @@ -184,10 +251,8 @@ class ScreenRecoveryUI : public RecoveryUI { bool show_text; bool show_text_ever; // has show_text ever been true? - std::vector menu_; - const char* const* menu_headers_; - bool show_menu; - int menu_items, menu_sel; + bool scrollable_menu_; + std::unique_ptr menu_; // An alternate text screen, swapped with 'text_' when we're viewing a log file. char** file_viewer_text_; diff --git a/tests/Android.mk b/tests/Android.mk index b3584fe87a..9a71371faf 100644 --- a/tests/Android.mk +++ b/tests/Android.mk @@ -23,6 +23,7 @@ LOCAL_MODULE := recovery_unit_test LOCAL_COMPATIBILITY_SUITE := device-tests LOCAL_STATIC_LIBRARIES := \ libverifier \ + librecovery_ui \ libminui \ libotautil \ libupdater \ @@ -38,8 +39,9 @@ LOCAL_SRC_FILES := \ unit/dirutil_test.cpp \ unit/locale_test.cpp \ unit/rangeset_test.cpp \ + unit/screen_ui_test.cpp \ unit/sysutil_test.cpp \ - unit/zip_test.cpp \ + unit/zip_test.cpp LOCAL_C_INCLUDES := bootable/recovery LOCAL_SHARED_LIBRARIES := liblog diff --git a/tests/unit/screen_ui_test.cpp b/tests/unit/screen_ui_test.cpp new file mode 100644 index 0000000000..be6799f2e0 --- /dev/null +++ b/tests/unit/screen_ui_test.cpp @@ -0,0 +1,198 @@ +/* + * Copyright (C) 2018 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 "screen_ui.h" + +#include + +#include + +constexpr const char* HEADER[] = { "header", nullptr }; +constexpr const char* ITEMS[] = { "items1", "items2", "items3", "items4", "1234567890", nullptr }; + +TEST(ScreenUITest, StartPhoneMenuSmoke) { + Menu menu(false, 10, 20); + ASSERT_FALSE(menu.scrollable()); + + menu.Start(HEADER, ITEMS, 0); + ASSERT_EQ(HEADER[0], menu.text_headers()[0]); + ASSERT_EQ(5u, menu.ItemsCount()); + + std::string message; + ASSERT_FALSE(menu.ItemsOverflow(&message)); + for (size_t i = 0; i < menu.ItemsCount(); i++) { + ASSERT_EQ(ITEMS[i], menu.TextItem(i)); + } + + ASSERT_EQ(0, menu.selection()); +} + +TEST(ScreenUITest, StartWearMenuSmoke) { + Menu menu(true, 10, 8); + ASSERT_TRUE(menu.scrollable()); + + menu.Start(HEADER, ITEMS, 1); + ASSERT_EQ(HEADER[0], menu.text_headers()[0]); + ASSERT_EQ(5u, menu.ItemsCount()); + + std::string message; + ASSERT_FALSE(menu.ItemsOverflow(&message)); + for (size_t i = 0; i < menu.ItemsCount() - 1; i++) { + ASSERT_EQ(ITEMS[i], menu.TextItem(i)); + } + // Test of the last item is truncated + ASSERT_EQ("12345678", menu.TextItem(4)); + ASSERT_EQ(1, menu.selection()); +} + +TEST(ScreenUITest, StartPhoneMenuItemsOverflow) { + Menu menu(false, 1, 20); + ASSERT_FALSE(menu.scrollable()); + + menu.Start(HEADER, ITEMS, 0); + ASSERT_EQ(1u, menu.ItemsCount()); + + std::string message; + ASSERT_FALSE(menu.ItemsOverflow(&message)); + for (size_t i = 0; i < menu.ItemsCount(); i++) { + ASSERT_EQ(ITEMS[i], menu.TextItem(i)); + } + + ASSERT_EQ(0u, menu.MenuStart()); + ASSERT_EQ(1u, menu.MenuEnd()); +} + +TEST(ScreenUITest, StartWearMenuItemsOverflow) { + Menu menu(true, 1, 20); + ASSERT_TRUE(menu.scrollable()); + + menu.Start(HEADER, ITEMS, 0); + ASSERT_EQ(5u, menu.ItemsCount()); + + std::string message; + ASSERT_TRUE(menu.ItemsOverflow(&message)); + ASSERT_EQ("Current item: 1/5", message); + + for (size_t i = 0; i < menu.ItemsCount(); i++) { + ASSERT_EQ(ITEMS[i], menu.TextItem(i)); + } + + ASSERT_EQ(0u, menu.MenuStart()); + ASSERT_EQ(1u, menu.MenuEnd()); +} + +TEST(ScreenUITest, PhoneMenuSelectSmoke) { + Menu menu(false, 10, 20); + + int sel = 0; + menu.Start(HEADER, ITEMS, sel); + // Mimic down button 10 times (2 * items size) + for (int i = 0; i < 10; i++) { + sel = menu.Select(++sel); + ASSERT_EQ(sel, menu.selection()); + + // Wraps the selection for unscrollable menu when it reaches the boundary. + int expected = (i + 1) % 5; + ASSERT_EQ(expected, menu.selection()); + + ASSERT_EQ(0u, menu.MenuStart()); + ASSERT_EQ(5u, menu.MenuEnd()); + } + + // Mimic up button 10 times + for (int i = 0; i < 10; i++) { + sel = menu.Select(--sel); + ASSERT_EQ(sel, menu.selection()); + + int expected = (9 - i) % 5; + ASSERT_EQ(expected, menu.selection()); + + ASSERT_EQ(0u, menu.MenuStart()); + ASSERT_EQ(5u, menu.MenuEnd()); + } +} + +TEST(ScreenUITest, WearMenuSelectSmoke) { + Menu menu(true, 10, 20); + + int sel = 0; + menu.Start(HEADER, ITEMS, sel); + // Mimic pressing down button 10 times (2 * items size) + for (int i = 0; i < 10; i++) { + sel = menu.Select(++sel); + ASSERT_EQ(sel, menu.selection()); + + // Stops the selection at the boundary if the menu is scrollable. + int expected = std::min(i + 1, 4); + ASSERT_EQ(expected, menu.selection()); + + ASSERT_EQ(0u, menu.MenuStart()); + ASSERT_EQ(5u, menu.MenuEnd()); + } + + // Mimic pressing up button 10 times + for (int i = 0; i < 10; i++) { + sel = menu.Select(--sel); + ASSERT_EQ(sel, menu.selection()); + + int expected = std::max(3 - i, 0); + ASSERT_EQ(expected, menu.selection()); + + ASSERT_EQ(0u, menu.MenuStart()); + ASSERT_EQ(5u, menu.MenuEnd()); + } +} + +TEST(ScreenUITest, WearMenuSelectItemsOverflow) { + Menu menu(true, 3, 20); + + int sel = 1; + menu.Start(HEADER, ITEMS, sel); + ASSERT_EQ(5u, menu.ItemsCount()); + + // Scroll the menu to the end, and check the start & end of menu. + for (int i = 0; i < 3; i++) { + sel = menu.Select(++sel); + ASSERT_EQ(i + 2, sel); + ASSERT_EQ(static_cast(i), menu.MenuStart()); + ASSERT_EQ(static_cast(i + 3), menu.MenuEnd()); + } + + // Press down button one more time won't change the MenuStart() and MenuEnd(). + sel = menu.Select(++sel); + ASSERT_EQ(4, sel); + ASSERT_EQ(2u, menu.MenuStart()); + ASSERT_EQ(5u, menu.MenuEnd()); + + // Scroll the menu to the top. + // The expected menu sel, start & ends are: + // sel 3, start 2, end 5 + // sel 2, start 2, end 5 + // sel 1, start 1, end 4 + // sel 0, start 0, end 3 + for (int i = 0; i < 4; i++) { + sel = menu.Select(--sel); + ASSERT_EQ(3 - i, sel); + ASSERT_EQ(static_cast(std::min(3 - i, 2)), menu.MenuStart()); + ASSERT_EQ(static_cast(std::min(6 - i, 5)), menu.MenuEnd()); + } + + // Press up button one more time won't change the MenuStart() and MenuEnd(). + sel = menu.Select(--sel); + ASSERT_EQ(0, sel); + ASSERT_EQ(0u, menu.MenuStart()); + ASSERT_EQ(3u, menu.MenuEnd()); +} diff --git a/wear_ui.cpp b/wear_ui.cpp index ca6b1b1028..118e435084 100644 --- a/wear_ui.cpp +++ b/wear_ui.cpp @@ -17,7 +17,6 @@ #include "wear_ui.h" #include -#include // TODO: Remove after killing the call to sprintf(). #include #include @@ -27,7 +26,8 @@ #include WearRecoveryUI::WearRecoveryUI() - : kProgressBarBaseline(RECOVERY_UI_PROGRESS_BAR_BASELINE), + : ScreenRecoveryUI(true), + kProgressBarBaseline(RECOVERY_UI_PROGRESS_BAR_BASELINE), kMenuUnusableRows(RECOVERY_UI_MENU_UNUSABLE_ROWS) { // TODO: kMenuUnusableRows should be computed based on the lines in draw_screen_locked(). @@ -65,13 +65,10 @@ static const char* SWIPE_HELP[] = { "Swipe up/down to move.", "Swipe left/right to select.", "", - NULL + nullptr, }; -// TODO merge drawing routines with screen_ui void WearRecoveryUI::draw_screen_locked() { - char cur_selection_str[50]; - draw_background_locked(); if (!show_text) { draw_foreground_locked(); @@ -79,68 +76,7 @@ void WearRecoveryUI::draw_screen_locked() { SetColor(TEXT_FILL); gr_fill(0, 0, gr_fb_width(), gr_fb_height()); - int y = kMarginHeight; - int x = kMarginWidth; - if (show_menu) { - std::string recovery_fingerprint = - android::base::GetProperty("ro.bootimage.build.fingerprint", ""); - SetColor(HEADER); - y += DrawTextLine(x + 4, y, "Android Recovery", true); - for (auto& chunk : android::base::Split(recovery_fingerprint, ":")) { - y += DrawTextLine(x + 4, y, chunk.c_str(), false); - } - - // This is actually the help strings. - y += DrawTextLines(x + 4, y, SWIPE_HELP); - SetColor(HEADER); - y += DrawTextLines(x + 4, y, menu_headers_); - - // Show the current menu item number in relation to total number if - // 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(gr_sys_font(), x + 4, y, cur_selection_str, 1); - y += char_height_ + 4; - } - - // Menu begins here - SetColor(MENU); - - for (int i = menu_start; i < menu_end; ++i) { - if (i == menu_sel) { - // draw the highlight bar - SetColor(MENU_SEL_BG); - 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(gr_sys_font(), x + 4, y, menu_[i].c_str(), 1); - } - SetColor(MENU); - } else if (menu_[i][0]) { - gr_text(gr_sys_font(), x + 4, y, menu_[i].c_str(), 0); - } - y += char_height_ + 4; - } - SetColor(MENU); - y += 4; - gr_fill(0, y, gr_fb_width(), y + 2); - y += 4; - } - - SetColor(LOG); - - // display from the bottom up, until we hit the top of the - // screen, the bottom of the menu, or we've displayed the - // entire text buffer. - int row = text_row_; - size_t count = 0; - for (int ty = gr_fb_height() - char_height_ - kMarginHeight; ty > y + 2 && count < text_rows_; - ty -= char_height_, ++count) { - gr_text(gr_sys_font(), x + 4, ty, text_[row], 0); - --row; - if (row < 0) row = text_rows_ - 1; - } + draw_menu_and_text_buffer_locked(SWIPE_HELP); } } @@ -156,45 +92,11 @@ void WearRecoveryUI::StartMenu(const char* const* headers, const char* const* it int initial_selection) { pthread_mutex_lock(&updateMutex); if (text_rows_ > 0 && text_cols_ > 0) { - menu_headers_ = headers; - menu_.clear(); - // "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. - // Bug: 23752519 - for (size_t i = 0; items[i] != nullptr; i++) { - menu_.emplace_back(std::string(items[i], strnlen(items[i], text_cols_ - 1))); - } - menu_items = static_cast(menu_.size()); - show_menu = true; - menu_sel = initial_selection; - menu_start = 0; - menu_end = text_rows_ - 1 - kMenuUnusableRows; - if (menu_items <= menu_end) menu_end = menu_items; - update_screen_locked(); - } - pthread_mutex_unlock(&updateMutex); -} + menu_ = std::make_unique(scrollable_menu_, text_rows_ - kMenuUnusableRows - 1, + text_cols_ - 1); + menu_->Start(headers, items, initial_selection); -int WearRecoveryUI::SelectMenu(int sel) { - int old_sel; - pthread_mutex_lock(&updateMutex); - if (show_menu) { - old_sel = menu_sel; - menu_sel = sel; - if (menu_sel < 0) menu_sel = 0; - if (menu_sel >= menu_items) menu_sel = menu_items - 1; - if (menu_sel < menu_start) { - menu_start--; - menu_end--; - } else if (menu_sel >= menu_end && menu_sel < menu_items) { - menu_end++; - menu_start++; - } - sel = menu_sel; - if (menu_sel != old_sel) update_screen_locked(); + update_screen_locked(); } pthread_mutex_unlock(&updateMutex); - return sel; -} +} \ No newline at end of file diff --git a/wear_ui.h b/wear_ui.h index 739b4cb1d8..8b24cb73e4 100644 --- a/wear_ui.h +++ b/wear_ui.h @@ -25,10 +25,8 @@ class WearRecoveryUI : public ScreenRecoveryUI { void SetStage(int current, int max) override; - // menu display void StartMenu(const char* const* headers, const char* const* items, int initial_selection) override; - int SelectMenu(int sel) override; protected: // progress bar vertical position, it's centered horizontally @@ -45,8 +43,6 @@ class WearRecoveryUI : public ScreenRecoveryUI { private: void draw_background_locked() override; void draw_screen_locked() override; - - int menu_start, menu_end; }; #endif // RECOVERY_WEAR_UI_H From 5fe5eb67efc7554d788276b514e724e4dfd01232 Mon Sep 17 00:00:00 2001 From: Tianjie Xu Date: Tue, 20 Mar 2018 16:07:39 -0700 Subject: [PATCH 012/560] Factor out a menu class for screen ui Also consolidate the duplicate codes to draw the menu in ScreenRecoveryUI and WearRecoveryUI. This helps us to support text icons as menu in the future. Bug: 74397117 Test: Check the menu under recovery on bullhead and a wear device. Change-Id: Iba9b646c3828670f0e78a7e07d1a94a44e96bb0b Merged-In: Iba9b646c3828670f0e78a7e07d1a94a44e96bb0b --- Android.mk | 79 ++++++++------ screen_ui.cpp | 183 ++++++++++++++++++++++++------- screen_ui.h | 73 ++++++++++++- tests/Android.mk | 4 +- tests/unit/screen_ui_test.cpp | 198 ++++++++++++++++++++++++++++++++++ wear_ui.cpp | 116 ++------------------ wear_ui.h | 4 - 7 files changed, 472 insertions(+), 185 deletions(-) create mode 100644 tests/unit/screen_ui_test.cpp diff --git a/Android.mk b/Android.mk index c8a21e084e..b289296042 100644 --- a/Android.mk +++ b/Android.mk @@ -53,36 +53,21 @@ LOCAL_STATIC_LIBRARIES := \ include $(BUILD_STATIC_LIBRARY) -# recovery (static executable) +# librecovery_ui (static library) # =============================== include $(CLEAR_VARS) - LOCAL_SRC_FILES := \ - adb_install.cpp \ - device.cpp \ - fuse_sdcard_provider.cpp \ - recovery.cpp \ - roots.cpp \ - rotate_logs.cpp \ screen_ui.cpp \ ui.cpp \ vr_ui.cpp \ - wear_ui.cpp \ - -LOCAL_MODULE := recovery - -LOCAL_FORCE_STATIC_EXECUTABLE := true - -LOCAL_REQUIRED_MODULES := e2fsdroid_static mke2fs_static mke2fs.conf + wear_ui.cpp -ifeq ($(TARGET_USERIMAGES_USE_F2FS),true) -ifeq ($(HOST_OS),linux) -LOCAL_REQUIRED_MODULES += sload.f2fs mkfs.f2fs -endif -endif +LOCAL_CFLAGS := -Wall -Werror -LOCAL_CFLAGS += -DRECOVERY_API_VERSION=$(RECOVERY_API_VERSION) -LOCAL_CFLAGS += -Wall -Werror +LOCAL_MODULE := librecovery_ui +LOCAL_STATIC_LIBRARIES := \ + libminui \ + libbase ifneq ($(TARGET_RECOVERY_UI_MARGIN_HEIGHT),) LOCAL_CFLAGS += -DRECOVERY_UI_MARGIN_HEIGHT=$(TARGET_RECOVERY_UI_MARGIN_HEIGHT) @@ -132,11 +117,50 @@ else LOCAL_CFLAGS += -DRECOVERY_UI_VR_STEREO_OFFSET=0 endif +include $(BUILD_STATIC_LIBRARY) + +# recovery (static executable) +# =============================== +include $(CLEAR_VARS) + +LOCAL_SRC_FILES := \ + adb_install.cpp \ + device.cpp \ + fuse_sdcard_provider.cpp \ + recovery.cpp \ + roots.cpp \ + rotate_logs.cpp \ + + +LOCAL_MODULE := recovery + +LOCAL_FORCE_STATIC_EXECUTABLE := true + +LOCAL_REQUIRED_MODULES := e2fsdroid_static mke2fs_static mke2fs.conf + +ifeq ($(TARGET_USERIMAGES_USE_F2FS),true) +ifeq ($(HOST_OS),linux) +LOCAL_REQUIRED_MODULES += sload.f2fs mkfs.f2fs +endif +endif + +LOCAL_CFLAGS += -DRECOVERY_API_VERSION=$(RECOVERY_API_VERSION) +LOCAL_CFLAGS += -Wall -Werror + LOCAL_C_INCLUDES += \ system/vold \ -LOCAL_STATIC_LIBRARIES := \ - librecovery \ +LOCAL_STATIC_LIBRARIES := librecovery + +# If $(TARGET_RECOVERY_UI_LIB) is defined, the recovery calls make_device() from the +# $(TARGET_RECOVERY_UI_LIB), which depends on the librecovery_ui. +ifeq ($(TARGET_RECOVERY_UI_LIB),) + LOCAL_SRC_FILES += default_device.cpp +else + LOCAL_STATIC_LIBRARIES += $(TARGET_RECOVERY_UI_LIB) +endif + +LOCAL_STATIC_LIBRARIES += \ libverifier \ libbatterymonitor \ libbootloader_message \ @@ -149,6 +173,7 @@ LOCAL_STATIC_LIBRARIES := \ libminadbd \ libasyncio \ libfusesideload \ + librecovery_ui \ libminui \ libpng \ libcrypto_utils \ @@ -172,12 +197,6 @@ endif LOCAL_MODULE_PATH := $(TARGET_RECOVERY_ROOT_OUT)/sbin -ifeq ($(TARGET_RECOVERY_UI_LIB),) - LOCAL_SRC_FILES += default_device.cpp -else - LOCAL_STATIC_LIBRARIES += $(TARGET_RECOVERY_UI_LIB) -endif - ifeq ($(BOARD_CACHEIMAGE_PARTITION_SIZE),) LOCAL_REQUIRED_MODULES += recovery-persist recovery-refresh endif diff --git a/screen_ui.cpp b/screen_ui.cpp index c8fb5aa755..317e5529ce 100644 --- a/screen_ui.cpp +++ b/screen_ui.cpp @@ -53,7 +53,98 @@ static double now() { return tv.tv_sec + tv.tv_usec / 1000000.0; } -ScreenRecoveryUI::ScreenRecoveryUI() +Menu::Menu(bool scrollable, size_t max_items, size_t max_length) + : scrollable_(scrollable), + max_display_items_(max_items), + max_item_length_(max_length), + text_headers_(nullptr), + menu_start_(0), + selection_(0) { + CHECK_LE(max_items, static_cast(std::numeric_limits::max())); +} + +const char* const* Menu::text_headers() const { + return text_headers_; +} + +std::string Menu::TextItem(size_t index) const { + CHECK_LT(index, text_items_.size()); + + return text_items_[index]; +} + +size_t Menu::MenuStart() const { + return menu_start_; +} + +size_t Menu::MenuEnd() const { + return std::min(ItemsCount(), menu_start_ + max_display_items_); +} + +size_t Menu::ItemsCount() const { + return text_items_.size(); +} + +bool Menu::ItemsOverflow(std::string* cur_selection_str) const { + if (!scrollable_ || static_cast(ItemsCount()) <= max_display_items_) { + return false; + } + + *cur_selection_str = + android::base::StringPrintf("Current item: %d/%zu", selection_ + 1, ItemsCount()); + return true; +} + +void Menu::Start(const char* const* headers, const char* const* items, int initial_selection) { + text_headers_ = headers; + + // It's fine to have more entries than text_rows_ if scrollable menu is supported. + size_t max_items_count = scrollable_ ? std::numeric_limits::max() : max_display_items_; + for (size_t i = 0; i < max_items_count && items[i] != nullptr; ++i) { + text_items_.emplace_back(items[i], strnlen(items[i], max_item_length_)); + } + + CHECK(!text_items_.empty()); + selection_ = initial_selection; +} + +// TODO(xunchang) modify the function parameters to button up & down. +int Menu::Select(int sel) { + CHECK_LE(ItemsCount(), static_cast(std::numeric_limits::max())); + int count = ItemsCount(); + + // Wraps the selection at boundary if the menu is not scrollable. + if (!scrollable_) { + if (sel < 0) { + selection_ = count - 1; + } else if (sel >= count) { + selection_ = 0; + } else { + selection_ = sel; + } + + return selection_; + } + + if (sel < 0) { + selection_ = 0; + } else if (sel >= count) { + selection_ = count - 1; + } else { + if (static_cast(sel) < menu_start_) { + menu_start_--; + } else if (static_cast(sel) >= MenuEnd()) { + menu_start_++; + } + selection_ = sel; + } + + return selection_; +} + +ScreenRecoveryUI::ScreenRecoveryUI() : ScreenRecoveryUI(false) {} + +ScreenRecoveryUI::ScreenRecoveryUI(bool scrollable_menu) : kMarginWidth(RECOVERY_UI_MARGIN_WIDTH), kMarginHeight(RECOVERY_UI_MARGIN_HEIGHT), kAnimationFps(RECOVERY_UI_ANIMATION_FPS), @@ -71,10 +162,7 @@ ScreenRecoveryUI::ScreenRecoveryUI() text_row_(0), show_text(false), show_text_ever(false), - menu_headers_(nullptr), - show_menu(false), - menu_items(0), - menu_sel(0), + scrollable_menu_(scrollable_menu), file_viewer_text_(nullptr), intro_frames(0), loop_frames(0), @@ -407,13 +495,13 @@ int ScreenRecoveryUI::DrawWrappedTextLines(int x, int y, const char* const* line static const char* REGULAR_HELP[] = { "Use volume up/down and power.", - NULL + nullptr, }; static const char* LONG_PRESS_HELP[] = { "Any button cycles highlight.", "Long-press activates.", - NULL + nullptr, }; // Redraws everything on the screen. Does not flip pages. Should only be called with updateMutex @@ -428,8 +516,13 @@ void ScreenRecoveryUI::draw_screen_locked() { gr_color(0, 0, 0, 255); gr_clear(); + draw_menu_and_text_buffer_locked(HasThreeButtons() ? REGULAR_HELP : LONG_PRESS_HELP); +} + +// Draws the menu and text buffer on the screen. Should only be called with updateMutex locked. +void ScreenRecoveryUI::draw_menu_and_text_buffer_locked(const char* const* help_message) { int y = kMarginHeight; - if (show_menu) { + if (menu_) { static constexpr int kMenuIndent = 4; int x = kMarginWidth + kMenuIndent; @@ -440,26 +533,46 @@ void ScreenRecoveryUI::draw_screen_locked() { for (const auto& chunk : android::base::Split(recovery_fingerprint, ":")) { y += DrawTextLine(x, y, chunk.c_str(), false); } - y += DrawTextLines(x, y, HasThreeButtons() ? REGULAR_HELP : LONG_PRESS_HELP); + y += DrawTextLines(x, y, help_message); + + // Draw menu header. SetColor(HEADER); - // Ignore kMenuIndent, which is not taken into account by text_cols_. - y += DrawWrappedTextLines(kMarginWidth, y, menu_headers_); + if (!menu_->scrollable()) { + y += DrawWrappedTextLines(x, y, menu_->text_headers()); + } else { + y += DrawTextLines(x, y, menu_->text_headers()); + // Show the current menu item number in relation to total number if items don't fit on the + // screen. + std::string cur_selection_str; + if (menu_->ItemsOverflow(&cur_selection_str)) { + y += DrawTextLine(x, y, cur_selection_str.c_str(), true); + } + } + // Draw menu items. SetColor(MENU); - y += DrawHorizontalRule(y) + 4; - for (int i = 0; i < menu_items; ++i) { - if (i == menu_sel) { + // Do not draw the horizontal rule for wear devices. + if (!menu_->scrollable()) { + y += DrawHorizontalRule(y) + 4; + } + for (size_t i = menu_->MenuStart(); i < menu_->MenuEnd(); ++i) { + bool bold = false; + if (i == static_cast(menu_->selection())) { // Draw the highlight bar. SetColor(IsLongPress() ? MENU_SEL_BG_ACTIVE : MENU_SEL_BG); - DrawHighlightBar(0, y - 2, ScreenWidth(), char_height_ + 4); + + int bar_height = char_height_ + 4; + DrawHighlightBar(0, y - 2, ScreenWidth(), bar_height); + // Bold white text for the selected item. SetColor(MENU_SEL_FG); - y += DrawTextLine(x, y, menu_[i].c_str(), true); - SetColor(MENU); - } else { - y += DrawTextLine(x, y, menu_[i].c_str(), false); + bold = true; } + + y += DrawTextLine(x, y, menu_->TextItem(i).c_str(), bold); + + SetColor(MENU); } y += DrawHorizontalRule(y); } @@ -864,15 +977,10 @@ void ScreenRecoveryUI::ShowFile(const char* filename) { void ScreenRecoveryUI::StartMenu(const char* const* headers, const char* const* items, int initial_selection) { pthread_mutex_lock(&updateMutex); - if (text_rows_ > 0 && text_cols_ > 0) { - menu_headers_ = headers; - menu_.clear(); - for (size_t i = 0; i < text_rows_ && items[i] != nullptr; ++i) { - menu_.emplace_back(std::string(items[i], strnlen(items[i], text_cols_ - 1))); - } - menu_items = static_cast(menu_.size()); - show_menu = true; - menu_sel = initial_selection; + if (text_rows_ > 0 && text_cols_ > 1) { + menu_ = std::make_unique(scrollable_menu_, text_rows_, text_cols_ - 1); + menu_->Start(headers, items, initial_selection); + update_screen_locked(); } pthread_mutex_unlock(&updateMutex); @@ -880,16 +988,13 @@ void ScreenRecoveryUI::StartMenu(const char* const* headers, const char* const* int ScreenRecoveryUI::SelectMenu(int sel) { pthread_mutex_lock(&updateMutex); - if (show_menu) { - int old_sel = menu_sel; - menu_sel = sel; - - // Wrap at top and bottom. - if (menu_sel < 0) menu_sel = menu_items - 1; - if (menu_sel >= menu_items) menu_sel = 0; + if (menu_) { + int old_sel = menu_->selection(); + sel = menu_->Select(sel); - sel = menu_sel; - if (menu_sel != old_sel) update_screen_locked(); + if (sel != old_sel) { + update_screen_locked(); + } } pthread_mutex_unlock(&updateMutex); return sel; @@ -897,8 +1002,8 @@ int ScreenRecoveryUI::SelectMenu(int sel) { void ScreenRecoveryUI::EndMenu() { pthread_mutex_lock(&updateMutex); - if (show_menu && text_rows_ > 0 && text_cols_ > 0) { - show_menu = false; + if (menu_) { + menu_.reset(); update_screen_locked(); } pthread_mutex_unlock(&updateMutex); diff --git a/screen_ui.h b/screen_ui.h index f05761c42b..c1222a5768 100644 --- a/screen_ui.h +++ b/screen_ui.h @@ -20,6 +20,7 @@ #include #include +#include #include #include @@ -28,6 +29,70 @@ // From minui/minui.h. struct GRSurface; +// This class maintains the menu selection and display of the screen ui. +class Menu { + public: + Menu(bool scrollable, size_t max_items, size_t max_length); + + bool scrollable() const { + return scrollable_; + } + + int selection() const { + return selection_; + } + + // Returns count of menu items. + size_t ItemsCount() const; + // Returns the index of the first menu item. + size_t MenuStart() const; + // Returns the index of the last menu item + 1. + size_t MenuEnd() const; + + // Menu example: + // info: Android Recovery + // .... + // help messages: Swipe up/down to move + // Swipe left/right to select + // empty line (horizontal rule): + // menu headers: Select file to view + // menu items: /cache/recovery/last_log + // /cache/recovery/last_log.1 + // /cache/recovery/last_log.2 + // ... + const char* const* text_headers() const; + std::string TextItem(size_t index) const; + + // Checks if the menu items fit vertically on the screen. Returns true and set the + // |cur_selection_str| if the items exceed the screen limit. + bool ItemsOverflow(std::string* cur_selection_str) const; + + // Starts the menu with |headers| and |items| in text. Sets the default selection to + // |initial_selection|. + void Start(const char* const* headers, const char* const* items, int initial_selection); + + // Sets the current selection to |sel|. Handle the overflow cases depending on if the menu is + // scrollable. + int Select(int sel); + + private: + // The menu is scrollable to display more items. Used on wear devices who have smaller screens. + const bool scrollable_; + // The max number of menu items to fit vertically on a screen. + const size_t max_display_items_; + // The length of each item to fit horizontally on a screen. + const size_t max_item_length_; + + // Internal storage for the menu headers and items in text. + const char* const* text_headers_; + std::vector text_items_; + + // The first item to display on the screen. + size_t menu_start_; + // Current menu selection. + int selection_; +}; + // Implementation of RecoveryUI appropriate for devices with a screen // (shows an icon + a progress bar, text logging, menu, etc.) class ScreenRecoveryUI : public RecoveryUI { @@ -44,6 +109,7 @@ class ScreenRecoveryUI : public RecoveryUI { }; ScreenRecoveryUI(); + explicit ScreenRecoveryUI(bool scrollable_menu); bool Init(const std::string& locale) override; @@ -101,6 +167,7 @@ class ScreenRecoveryUI : public RecoveryUI { virtual void draw_background_locked(); virtual void draw_foreground_locked(); virtual void draw_screen_locked(); + virtual void draw_menu_and_text_buffer_locked(const char* const* help_message); virtual void update_screen_locked(); virtual void update_progress_locked(); @@ -184,10 +251,8 @@ class ScreenRecoveryUI : public RecoveryUI { bool show_text; bool show_text_ever; // has show_text ever been true? - std::vector menu_; - const char* const* menu_headers_; - bool show_menu; - int menu_items, menu_sel; + bool scrollable_menu_; + std::unique_ptr menu_; // An alternate text screen, swapped with 'text_' when we're viewing a log file. char** file_viewer_text_; diff --git a/tests/Android.mk b/tests/Android.mk index b3584fe87a..9a71371faf 100644 --- a/tests/Android.mk +++ b/tests/Android.mk @@ -23,6 +23,7 @@ LOCAL_MODULE := recovery_unit_test LOCAL_COMPATIBILITY_SUITE := device-tests LOCAL_STATIC_LIBRARIES := \ libverifier \ + librecovery_ui \ libminui \ libotautil \ libupdater \ @@ -38,8 +39,9 @@ LOCAL_SRC_FILES := \ unit/dirutil_test.cpp \ unit/locale_test.cpp \ unit/rangeset_test.cpp \ + unit/screen_ui_test.cpp \ unit/sysutil_test.cpp \ - unit/zip_test.cpp \ + unit/zip_test.cpp LOCAL_C_INCLUDES := bootable/recovery LOCAL_SHARED_LIBRARIES := liblog diff --git a/tests/unit/screen_ui_test.cpp b/tests/unit/screen_ui_test.cpp new file mode 100644 index 0000000000..be6799f2e0 --- /dev/null +++ b/tests/unit/screen_ui_test.cpp @@ -0,0 +1,198 @@ +/* + * Copyright (C) 2018 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 "screen_ui.h" + +#include + +#include + +constexpr const char* HEADER[] = { "header", nullptr }; +constexpr const char* ITEMS[] = { "items1", "items2", "items3", "items4", "1234567890", nullptr }; + +TEST(ScreenUITest, StartPhoneMenuSmoke) { + Menu menu(false, 10, 20); + ASSERT_FALSE(menu.scrollable()); + + menu.Start(HEADER, ITEMS, 0); + ASSERT_EQ(HEADER[0], menu.text_headers()[0]); + ASSERT_EQ(5u, menu.ItemsCount()); + + std::string message; + ASSERT_FALSE(menu.ItemsOverflow(&message)); + for (size_t i = 0; i < menu.ItemsCount(); i++) { + ASSERT_EQ(ITEMS[i], menu.TextItem(i)); + } + + ASSERT_EQ(0, menu.selection()); +} + +TEST(ScreenUITest, StartWearMenuSmoke) { + Menu menu(true, 10, 8); + ASSERT_TRUE(menu.scrollable()); + + menu.Start(HEADER, ITEMS, 1); + ASSERT_EQ(HEADER[0], menu.text_headers()[0]); + ASSERT_EQ(5u, menu.ItemsCount()); + + std::string message; + ASSERT_FALSE(menu.ItemsOverflow(&message)); + for (size_t i = 0; i < menu.ItemsCount() - 1; i++) { + ASSERT_EQ(ITEMS[i], menu.TextItem(i)); + } + // Test of the last item is truncated + ASSERT_EQ("12345678", menu.TextItem(4)); + ASSERT_EQ(1, menu.selection()); +} + +TEST(ScreenUITest, StartPhoneMenuItemsOverflow) { + Menu menu(false, 1, 20); + ASSERT_FALSE(menu.scrollable()); + + menu.Start(HEADER, ITEMS, 0); + ASSERT_EQ(1u, menu.ItemsCount()); + + std::string message; + ASSERT_FALSE(menu.ItemsOverflow(&message)); + for (size_t i = 0; i < menu.ItemsCount(); i++) { + ASSERT_EQ(ITEMS[i], menu.TextItem(i)); + } + + ASSERT_EQ(0u, menu.MenuStart()); + ASSERT_EQ(1u, menu.MenuEnd()); +} + +TEST(ScreenUITest, StartWearMenuItemsOverflow) { + Menu menu(true, 1, 20); + ASSERT_TRUE(menu.scrollable()); + + menu.Start(HEADER, ITEMS, 0); + ASSERT_EQ(5u, menu.ItemsCount()); + + std::string message; + ASSERT_TRUE(menu.ItemsOverflow(&message)); + ASSERT_EQ("Current item: 1/5", message); + + for (size_t i = 0; i < menu.ItemsCount(); i++) { + ASSERT_EQ(ITEMS[i], menu.TextItem(i)); + } + + ASSERT_EQ(0u, menu.MenuStart()); + ASSERT_EQ(1u, menu.MenuEnd()); +} + +TEST(ScreenUITest, PhoneMenuSelectSmoke) { + Menu menu(false, 10, 20); + + int sel = 0; + menu.Start(HEADER, ITEMS, sel); + // Mimic down button 10 times (2 * items size) + for (int i = 0; i < 10; i++) { + sel = menu.Select(++sel); + ASSERT_EQ(sel, menu.selection()); + + // Wraps the selection for unscrollable menu when it reaches the boundary. + int expected = (i + 1) % 5; + ASSERT_EQ(expected, menu.selection()); + + ASSERT_EQ(0u, menu.MenuStart()); + ASSERT_EQ(5u, menu.MenuEnd()); + } + + // Mimic up button 10 times + for (int i = 0; i < 10; i++) { + sel = menu.Select(--sel); + ASSERT_EQ(sel, menu.selection()); + + int expected = (9 - i) % 5; + ASSERT_EQ(expected, menu.selection()); + + ASSERT_EQ(0u, menu.MenuStart()); + ASSERT_EQ(5u, menu.MenuEnd()); + } +} + +TEST(ScreenUITest, WearMenuSelectSmoke) { + Menu menu(true, 10, 20); + + int sel = 0; + menu.Start(HEADER, ITEMS, sel); + // Mimic pressing down button 10 times (2 * items size) + for (int i = 0; i < 10; i++) { + sel = menu.Select(++sel); + ASSERT_EQ(sel, menu.selection()); + + // Stops the selection at the boundary if the menu is scrollable. + int expected = std::min(i + 1, 4); + ASSERT_EQ(expected, menu.selection()); + + ASSERT_EQ(0u, menu.MenuStart()); + ASSERT_EQ(5u, menu.MenuEnd()); + } + + // Mimic pressing up button 10 times + for (int i = 0; i < 10; i++) { + sel = menu.Select(--sel); + ASSERT_EQ(sel, menu.selection()); + + int expected = std::max(3 - i, 0); + ASSERT_EQ(expected, menu.selection()); + + ASSERT_EQ(0u, menu.MenuStart()); + ASSERT_EQ(5u, menu.MenuEnd()); + } +} + +TEST(ScreenUITest, WearMenuSelectItemsOverflow) { + Menu menu(true, 3, 20); + + int sel = 1; + menu.Start(HEADER, ITEMS, sel); + ASSERT_EQ(5u, menu.ItemsCount()); + + // Scroll the menu to the end, and check the start & end of menu. + for (int i = 0; i < 3; i++) { + sel = menu.Select(++sel); + ASSERT_EQ(i + 2, sel); + ASSERT_EQ(static_cast(i), menu.MenuStart()); + ASSERT_EQ(static_cast(i + 3), menu.MenuEnd()); + } + + // Press down button one more time won't change the MenuStart() and MenuEnd(). + sel = menu.Select(++sel); + ASSERT_EQ(4, sel); + ASSERT_EQ(2u, menu.MenuStart()); + ASSERT_EQ(5u, menu.MenuEnd()); + + // Scroll the menu to the top. + // The expected menu sel, start & ends are: + // sel 3, start 2, end 5 + // sel 2, start 2, end 5 + // sel 1, start 1, end 4 + // sel 0, start 0, end 3 + for (int i = 0; i < 4; i++) { + sel = menu.Select(--sel); + ASSERT_EQ(3 - i, sel); + ASSERT_EQ(static_cast(std::min(3 - i, 2)), menu.MenuStart()); + ASSERT_EQ(static_cast(std::min(6 - i, 5)), menu.MenuEnd()); + } + + // Press up button one more time won't change the MenuStart() and MenuEnd(). + sel = menu.Select(--sel); + ASSERT_EQ(0, sel); + ASSERT_EQ(0u, menu.MenuStart()); + ASSERT_EQ(3u, menu.MenuEnd()); +} diff --git a/wear_ui.cpp b/wear_ui.cpp index ca6b1b1028..118e435084 100644 --- a/wear_ui.cpp +++ b/wear_ui.cpp @@ -17,7 +17,6 @@ #include "wear_ui.h" #include -#include // TODO: Remove after killing the call to sprintf(). #include #include @@ -27,7 +26,8 @@ #include WearRecoveryUI::WearRecoveryUI() - : kProgressBarBaseline(RECOVERY_UI_PROGRESS_BAR_BASELINE), + : ScreenRecoveryUI(true), + kProgressBarBaseline(RECOVERY_UI_PROGRESS_BAR_BASELINE), kMenuUnusableRows(RECOVERY_UI_MENU_UNUSABLE_ROWS) { // TODO: kMenuUnusableRows should be computed based on the lines in draw_screen_locked(). @@ -65,13 +65,10 @@ static const char* SWIPE_HELP[] = { "Swipe up/down to move.", "Swipe left/right to select.", "", - NULL + nullptr, }; -// TODO merge drawing routines with screen_ui void WearRecoveryUI::draw_screen_locked() { - char cur_selection_str[50]; - draw_background_locked(); if (!show_text) { draw_foreground_locked(); @@ -79,68 +76,7 @@ void WearRecoveryUI::draw_screen_locked() { SetColor(TEXT_FILL); gr_fill(0, 0, gr_fb_width(), gr_fb_height()); - int y = kMarginHeight; - int x = kMarginWidth; - if (show_menu) { - std::string recovery_fingerprint = - android::base::GetProperty("ro.bootimage.build.fingerprint", ""); - SetColor(HEADER); - y += DrawTextLine(x + 4, y, "Android Recovery", true); - for (auto& chunk : android::base::Split(recovery_fingerprint, ":")) { - y += DrawTextLine(x + 4, y, chunk.c_str(), false); - } - - // This is actually the help strings. - y += DrawTextLines(x + 4, y, SWIPE_HELP); - SetColor(HEADER); - y += DrawTextLines(x + 4, y, menu_headers_); - - // Show the current menu item number in relation to total number if - // 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(gr_sys_font(), x + 4, y, cur_selection_str, 1); - y += char_height_ + 4; - } - - // Menu begins here - SetColor(MENU); - - for (int i = menu_start; i < menu_end; ++i) { - if (i == menu_sel) { - // draw the highlight bar - SetColor(MENU_SEL_BG); - 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(gr_sys_font(), x + 4, y, menu_[i].c_str(), 1); - } - SetColor(MENU); - } else if (menu_[i][0]) { - gr_text(gr_sys_font(), x + 4, y, menu_[i].c_str(), 0); - } - y += char_height_ + 4; - } - SetColor(MENU); - y += 4; - gr_fill(0, y, gr_fb_width(), y + 2); - y += 4; - } - - SetColor(LOG); - - // display from the bottom up, until we hit the top of the - // screen, the bottom of the menu, or we've displayed the - // entire text buffer. - int row = text_row_; - size_t count = 0; - for (int ty = gr_fb_height() - char_height_ - kMarginHeight; ty > y + 2 && count < text_rows_; - ty -= char_height_, ++count) { - gr_text(gr_sys_font(), x + 4, ty, text_[row], 0); - --row; - if (row < 0) row = text_rows_ - 1; - } + draw_menu_and_text_buffer_locked(SWIPE_HELP); } } @@ -156,45 +92,11 @@ void WearRecoveryUI::StartMenu(const char* const* headers, const char* const* it int initial_selection) { pthread_mutex_lock(&updateMutex); if (text_rows_ > 0 && text_cols_ > 0) { - menu_headers_ = headers; - menu_.clear(); - // "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. - // Bug: 23752519 - for (size_t i = 0; items[i] != nullptr; i++) { - menu_.emplace_back(std::string(items[i], strnlen(items[i], text_cols_ - 1))); - } - menu_items = static_cast(menu_.size()); - show_menu = true; - menu_sel = initial_selection; - menu_start = 0; - menu_end = text_rows_ - 1 - kMenuUnusableRows; - if (menu_items <= menu_end) menu_end = menu_items; - update_screen_locked(); - } - pthread_mutex_unlock(&updateMutex); -} + menu_ = std::make_unique(scrollable_menu_, text_rows_ - kMenuUnusableRows - 1, + text_cols_ - 1); + menu_->Start(headers, items, initial_selection); -int WearRecoveryUI::SelectMenu(int sel) { - int old_sel; - pthread_mutex_lock(&updateMutex); - if (show_menu) { - old_sel = menu_sel; - menu_sel = sel; - if (menu_sel < 0) menu_sel = 0; - if (menu_sel >= menu_items) menu_sel = menu_items - 1; - if (menu_sel < menu_start) { - menu_start--; - menu_end--; - } else if (menu_sel >= menu_end && menu_sel < menu_items) { - menu_end++; - menu_start++; - } - sel = menu_sel; - if (menu_sel != old_sel) update_screen_locked(); + update_screen_locked(); } pthread_mutex_unlock(&updateMutex); - return sel; -} +} \ No newline at end of file diff --git a/wear_ui.h b/wear_ui.h index 739b4cb1d8..8b24cb73e4 100644 --- a/wear_ui.h +++ b/wear_ui.h @@ -25,10 +25,8 @@ class WearRecoveryUI : public ScreenRecoveryUI { void SetStage(int current, int max) override; - // menu display void StartMenu(const char* const* headers, const char* const* items, int initial_selection) override; - int SelectMenu(int sel) override; protected: // progress bar vertical position, it's centered horizontally @@ -45,8 +43,6 @@ class WearRecoveryUI : public ScreenRecoveryUI { private: void draw_background_locked() override; void draw_screen_locked() override; - - int menu_start, menu_end; }; #endif // RECOVERY_WEAR_UI_H From f7e8012be3c2ac4460b577157e377cd695d411ef Mon Sep 17 00:00:00 2001 From: Tao Bao Date: Fri, 23 Mar 2018 22:01:43 -0700 Subject: [PATCH 013/560] Specify a default module for TARGET_RECOVERY_UI_LIB. This aligns the logic between {default,wear,vr}_device.cpp. They all implement their default make_device() functions, but using different RecoveryUI variants. This CL creates a new module librecovery_ui_default that uses ScreenRecoveryUI, which serves as the default value for TARGET_RECOVERY_UI_LIB. Test: lunch aosp_{angler,marlin,dragon,walleye}-userdebug; `mmma -j bootable/recovery` respectively. Change-Id: I6755b09d96e4809bb1c17142895fe1cad95e5a02 (cherry picked from commit 73fd036be8c7563423be0c3adbd4205c4f1bc2f5) --- Android.mk | 30 +++++++++++++++++++----------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/Android.mk b/Android.mk index 0499a6da8a..7359c9fbd3 100644 --- a/Android.mk +++ b/Android.mk @@ -18,6 +18,11 @@ LOCAL_PATH := $(call my-dir) RECOVERY_API_VERSION := 3 RECOVERY_FSTAB_VERSION := 2 +# TARGET_RECOVERY_UI_LIB should be one of librecovery_ui_{default,wear,vr} or a device-specific +# module that defines make_device() and the exact RecoveryUI class for the target. It defaults to +# librecovery_ui_default, which uses ScreenRecoveryUI. +TARGET_RECOVERY_UI_LIB ?= librecovery_ui_default + # libmounts (static library) # =============================== include $(CLEAR_VARS) @@ -32,8 +37,10 @@ include $(BUILD_STATIC_LIBRARY) # librecovery (static library) # =============================== include $(CLEAR_VARS) + LOCAL_SRC_FILES := \ install.cpp + LOCAL_CFLAGS := -Wall -Werror LOCAL_CFLAGS += -DRECOVERY_API_VERSION=$(RECOVERY_API_VERSION) @@ -131,7 +138,6 @@ LOCAL_SRC_FILES := \ roots.cpp \ rotate_logs.cpp \ - LOCAL_MODULE := recovery LOCAL_FORCE_STATIC_EXECUTABLE := true @@ -163,17 +169,9 @@ LOCAL_STATIC_LIBRARIES := \ libvndksupport \ libbatterymonitor -LOCAL_STATIC_LIBRARIES += librecovery - -# If $(TARGET_RECOVERY_UI_LIB) is defined, the recovery calls make_device() from the -# $(TARGET_RECOVERY_UI_LIB), which depends on the librecovery_ui. -ifeq ($(TARGET_RECOVERY_UI_LIB),) - LOCAL_SRC_FILES += default_device.cpp -else - LOCAL_STATIC_LIBRARIES += $(TARGET_RECOVERY_UI_LIB) -endif - LOCAL_STATIC_LIBRARIES += \ + librecovery \ + $(TARGET_RECOVERY_UI_LIB) \ libverifier \ libbootloader_message \ libfs_mgr \ @@ -254,6 +252,16 @@ LOCAL_STATIC_LIBRARIES := \ LOCAL_CFLAGS := -Wall -Werror include $(BUILD_STATIC_LIBRARY) +# Generic device that uses ScreenRecoveryUI. +# =============================== +include $(CLEAR_VARS) +LOCAL_SRC_FILES := default_device.cpp +LOCAL_CFLAGS := -Wall -Werror + +LOCAL_MODULE := librecovery_ui_default + +include $(BUILD_STATIC_LIBRARY) + # Wear default device # =============================== include $(CLEAR_VARS) From f6d25962bbcd06a16d965384458beed9c769d1b1 Mon Sep 17 00:00:00 2001 From: Tao Bao Date: Fri, 23 Mar 2018 22:01:43 -0700 Subject: [PATCH 014/560] Specify a default module for TARGET_RECOVERY_UI_LIB. This aligns the logic between {default,wear,vr}_device.cpp. They all implement their default make_device() functions, but using different RecoveryUI variants. This CL creates a new module librecovery_ui_default that uses ScreenRecoveryUI, which serves as the default value for TARGET_RECOVERY_UI_LIB. Test: lunch aosp_{angler,marlin,dragon,walleye}-userdebug; `mmma -j bootable/recovery` respectively. Change-Id: I6755b09d96e4809bb1c17142895fe1cad95e5a02 Merged-In: I6755b09d96e4809bb1c17142895fe1cad95e5a02 --- Android.mk | 32 ++++++++++++++++++++------------ 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/Android.mk b/Android.mk index b289296042..5dca32a453 100644 --- a/Android.mk +++ b/Android.mk @@ -18,6 +18,11 @@ LOCAL_PATH := $(call my-dir) RECOVERY_API_VERSION := 3 RECOVERY_FSTAB_VERSION := 2 +# TARGET_RECOVERY_UI_LIB should be one of librecovery_ui_{default,wear,vr} or a device-specific +# module that defines make_device() and the exact RecoveryUI class for the target. It defaults to +# librecovery_ui_default, which uses ScreenRecoveryUI. +TARGET_RECOVERY_UI_LIB ?= librecovery_ui_default + # libmounts (static library) # =============================== include $(CLEAR_VARS) @@ -32,8 +37,10 @@ include $(BUILD_STATIC_LIBRARY) # librecovery (static library) # =============================== include $(CLEAR_VARS) + LOCAL_SRC_FILES := \ install.cpp + LOCAL_CFLAGS := -Wall -Werror LOCAL_CFLAGS += -DRECOVERY_API_VERSION=$(RECOVERY_API_VERSION) @@ -131,7 +138,6 @@ LOCAL_SRC_FILES := \ roots.cpp \ rotate_logs.cpp \ - LOCAL_MODULE := recovery LOCAL_FORCE_STATIC_EXECUTABLE := true @@ -150,17 +156,9 @@ LOCAL_CFLAGS += -Wall -Werror LOCAL_C_INCLUDES += \ system/vold \ -LOCAL_STATIC_LIBRARIES := librecovery - -# If $(TARGET_RECOVERY_UI_LIB) is defined, the recovery calls make_device() from the -# $(TARGET_RECOVERY_UI_LIB), which depends on the librecovery_ui. -ifeq ($(TARGET_RECOVERY_UI_LIB),) - LOCAL_SRC_FILES += default_device.cpp -else - LOCAL_STATIC_LIBRARIES += $(TARGET_RECOVERY_UI_LIB) -endif - -LOCAL_STATIC_LIBRARIES += \ +LOCAL_STATIC_LIBRARIES := \ + librecovery \ + $(TARGET_RECOVERY_UI_LIB) \ libverifier \ libbatterymonitor \ libbootloader_message \ @@ -242,6 +240,16 @@ LOCAL_STATIC_LIBRARIES := \ LOCAL_CFLAGS := -Wall -Werror include $(BUILD_STATIC_LIBRARY) +# Generic device that uses ScreenRecoveryUI. +# =============================== +include $(CLEAR_VARS) +LOCAL_SRC_FILES := default_device.cpp +LOCAL_CFLAGS := -Wall -Werror + +LOCAL_MODULE := librecovery_ui_default + +include $(BUILD_STATIC_LIBRARY) + # Wear default device # =============================== include $(CLEAR_VARS) From d2f2ad6a3c10f1d9d908de39d19046efc53b53a0 Mon Sep 17 00:00:00 2001 From: Tao Bao Date: Fri, 23 Mar 2018 23:24:25 -0700 Subject: [PATCH 015/560] Move a few modules to Soong. libmounts librecovery_ui_default librecovery_ui_wear librecovery_ui_vr libverifier recovery-persist recovery-refresh They are all trivially converted. Test: mmma -j bootable/recovery Change-Id: Id783b3eec32bd15e74f42df17053732db5666675 --- Android.bp | 143 +++++++++++++++++++++++++++++++++++++++++++++++++++++ Android.mk | 85 +------------------------------ 2 files changed, 145 insertions(+), 83 deletions(-) create mode 100644 Android.bp diff --git a/Android.bp b/Android.bp new file mode 100644 index 0000000000..6c04504ea2 --- /dev/null +++ b/Android.bp @@ -0,0 +1,143 @@ +// Copyright (C) 2018 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. + +cc_defaults { + name: "recovery_defaults", + + cflags: [ + "-Wall", + "-Werror", + ], +} + +// Generic device that uses ScreenRecoveryUI. +cc_library_static { + name: "librecovery_ui_default", + + defaults: [ + "recovery_defaults", + ], + + srcs: [ + "default_device.cpp", + ], +} + +// The default wear device that uses WearRecoveryUI. +cc_library_static { + name: "librecovery_ui_wear", + + defaults: [ + "recovery_defaults", + ], + + srcs: [ + "wear_device.cpp", + ], +} + +// The default VR device that uses VrRecoveryUI. +cc_library_static { + name: "librecovery_ui_vr", + + defaults: [ + "recovery_defaults", + ], + + srcs: [ + "vr_device.cpp", + ], +} + +cc_library_static { + name: "libmounts", + + defaults: [ + "recovery_defaults", + ], + + srcs: [ + "mounts.cpp", + ], + + static_libs: [ + "libbase", + ], +} + +cc_library_static { + name: "libverifier", + + defaults: [ + "recovery_defaults", + ], + + srcs: [ + "asn1_decoder.cpp", + "verifier.cpp", + ], + + static_libs: [ + "libbase", + "libcrypto", + "libcrypto_utils", + "libotautil", + ], +} + +// The dynamic executable that runs after /data mounts. +cc_binary { + name: "recovery-persist", + + defaults: [ + "recovery_defaults", + ], + + srcs: [ + "recovery-persist.cpp", + "rotate_logs.cpp", + ], + + shared_libs: [ + "libbase", + "liblog", + ], + + init_rc: [ + "recovery-persist.rc", + ], +} + +// The dynamic executable that runs at init. +cc_binary { + name: "recovery-refresh", + + defaults: [ + "recovery_defaults", + ], + + srcs: [ + "recovery-refresh.cpp", + "rotate_logs.cpp", + ], + + shared_libs: [ + "libbase", + "liblog", + ], + + init_rc: [ + "recovery-refresh.rc", + ], +} diff --git a/Android.mk b/Android.mk index 5dca32a453..161dd322b3 100644 --- a/Android.mk +++ b/Android.mk @@ -23,17 +23,6 @@ RECOVERY_FSTAB_VERSION := 2 # librecovery_ui_default, which uses ScreenRecoveryUI. TARGET_RECOVERY_UI_LIB ?= librecovery_ui_default -# libmounts (static library) -# =============================== -include $(CLEAR_VARS) -LOCAL_SRC_FILES := mounts.cpp -LOCAL_CFLAGS := \ - -Wall \ - -Werror -LOCAL_MODULE := libmounts -LOCAL_STATIC_LIBRARIES := libbase -include $(BUILD_STATIC_LIBRARY) - # librecovery (static library) # =============================== include $(CLEAR_VARS) @@ -49,6 +38,7 @@ ifeq ($(AB_OTA_UPDATER),true) endif LOCAL_MODULE := librecovery + LOCAL_STATIC_LIBRARIES := \ libminui \ libotautil \ @@ -72,6 +62,7 @@ LOCAL_SRC_FILES := \ LOCAL_CFLAGS := -Wall -Werror LOCAL_MODULE := librecovery_ui + LOCAL_STATIC_LIBRARIES := \ libminui \ libbase @@ -201,78 +192,6 @@ endif include $(BUILD_EXECUTABLE) -# recovery-persist (system partition dynamic executable run after /data mounts) -# =============================== -include $(CLEAR_VARS) -LOCAL_SRC_FILES := \ - recovery-persist.cpp \ - rotate_logs.cpp -LOCAL_MODULE := recovery-persist -LOCAL_SHARED_LIBRARIES := liblog libbase -LOCAL_CFLAGS := -Wall -Werror -LOCAL_INIT_RC := recovery-persist.rc -include $(BUILD_EXECUTABLE) - -# recovery-refresh (system partition dynamic executable run at init) -# =============================== -include $(CLEAR_VARS) -LOCAL_SRC_FILES := \ - recovery-refresh.cpp \ - rotate_logs.cpp -LOCAL_MODULE := recovery-refresh -LOCAL_SHARED_LIBRARIES := liblog libbase -LOCAL_CFLAGS := -Wall -Werror -LOCAL_INIT_RC := recovery-refresh.rc -include $(BUILD_EXECUTABLE) - -# libverifier (static library) -# =============================== -include $(CLEAR_VARS) -LOCAL_MODULE := libverifier -LOCAL_SRC_FILES := \ - asn1_decoder.cpp \ - verifier.cpp -LOCAL_STATIC_LIBRARIES := \ - libotautil \ - libcrypto_utils \ - libcrypto \ - libbase -LOCAL_CFLAGS := -Wall -Werror -include $(BUILD_STATIC_LIBRARY) - -# Generic device that uses ScreenRecoveryUI. -# =============================== -include $(CLEAR_VARS) -LOCAL_SRC_FILES := default_device.cpp -LOCAL_CFLAGS := -Wall -Werror - -LOCAL_MODULE := librecovery_ui_default - -include $(BUILD_STATIC_LIBRARY) - -# Wear default device -# =============================== -include $(CLEAR_VARS) -LOCAL_SRC_FILES := wear_device.cpp -LOCAL_CFLAGS := -Wall -Werror - -# Should match TARGET_RECOVERY_UI_LIB in BoardConfig.mk. -LOCAL_MODULE := librecovery_ui_wear - -include $(BUILD_STATIC_LIBRARY) - -# vr headset default device -# =============================== -include $(CLEAR_VARS) - -LOCAL_SRC_FILES := vr_device.cpp -LOCAL_CFLAGS := -Wall -Werror - -# should match TARGET_RECOVERY_UI_LIB set in BoardConfig.mk -LOCAL_MODULE := librecovery_ui_vr - -include $(BUILD_STATIC_LIBRARY) - include \ $(LOCAL_PATH)/boot_control/Android.mk \ $(LOCAL_PATH)/minui/Android.mk \ From 55a2c4eaae521eabeee6c15cd17eb17de9b6afe5 Mon Sep 17 00:00:00 2001 From: Tianjie Xu Date: Thu, 29 Mar 2018 11:07:50 -0700 Subject: [PATCH 016/560] Remove the obsolete mkfont.c and font_10x18.h The font_10x18.h is used as a fall back for fonts if the $(recovery_font) under bootable/recovery/fonts failed to reload. This font looks very small on the screen and we haven't used it in a while. Plus, fonts in header doesn't have many benefits if we support only ascii characters. So we decide to remove this header along with the mkfont.c that generates the header. For reference, here's the steps to use mkfont: 1. Generate a picture of fonts with apps on host 2. Open the pic with GIMP 3. Export the pic as '.c' file, and rename it into '.h' file 4. Compile the generated '.h' file with mkfont.c Bug: 76420958 Test: build and check the recovery menu on angler Change-Id: I6c108aa43a07bf4994115ef764275bd84df725e1 --- minui/font_10x18.h | 214 --------------------------------------------- minui/graphics.cpp | 37 ++------ minui/mkfont.c | 54 ------------ 3 files changed, 5 insertions(+), 300 deletions(-) delete mode 100644 minui/font_10x18.h delete mode 100644 minui/mkfont.c diff --git a/minui/font_10x18.h b/minui/font_10x18.h deleted file mode 100644 index 30dfb9c563..0000000000 --- a/minui/font_10x18.h +++ /dev/null @@ -1,214 +0,0 @@ -struct { - unsigned width; - unsigned height; - unsigned char_width; - unsigned char_height; - unsigned char rundata[2973]; -} font = { - .width = 960, - .height = 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, -0x84,0x0a,0x81,0x03,0x88,0x04,0x84,0x04,0x88,0x04,0x84,0x06,0x84,0x1e,0x81, -0x0e,0x81,0x0a,0x84,0x06,0x84,0x07,0x82,0x05,0x85,0x07,0x84,0x04,0x86,0x04, -0x88,0x02,0x88,0x04,0x84,0x04,0x82,0x04,0x82,0x02,0x88,0x05,0x86,0x01,0x82, -0x04,0x82,0x02,0x82,0x08,0x82,0x04,0x82,0x02,0x82,0x04,0x82,0x04,0x84,0x04, -0x86,0x06,0x84,0x04,0x86,0x06,0x84,0x04,0x88,0x02,0x82,0x04,0x82,0x02,0x82, -0x04,0x82,0x02,0x82,0x04,0x82,0x02,0x82,0x04,0x82,0x02,0x82,0x04,0x82,0x02, -0x88,0x03,0x86,0x0e,0x86,0x06,0x82,0x11,0x82,0x10,0x82,0x18,0x82,0x0f,0x84, -0x0d,0x82,0x1c,0x82,0x09,0x84,0x7f,0x16,0x84,0x05,0x82,0x05,0x84,0x07,0x83, -0x02,0x82,0x19,0x82,0x06,0x82,0x02,0x82,0x06,0x82,0x01,0x82,0x03,0x86,0x04, -0x83,0x02,0x82,0x03,0x82,0x01,0x82,0x07,0x82,0x09,0x82,0x06,0x82,0x3e,0x82, -0x04,0x84,0x06,0x83,0x06,0x82,0x02,0x82,0x04,0x82,0x02,0x82,0x08,0x82,0x03, -0x82,0x09,0x82,0x02,0x82,0x09,0x82,0x03,0x82,0x02,0x82,0x04,0x82,0x02,0x82, -0x1c,0x82,0x0e,0x82,0x08,0x82,0x02,0x82,0x04,0x82,0x02,0x82,0x05,0x84,0x04, -0x82,0x02,0x82,0x05,0x82,0x02,0x82,0x03,0x82,0x03,0x82,0x03,0x82,0x08,0x82, -0x09,0x82,0x02,0x82,0x03,0x82,0x04,0x82,0x05,0x82,0x0a,0x82,0x03,0x82,0x04, -0x82,0x02,0x82,0x08,0x82,0x04,0x82,0x02,0x83,0x03,0x82,0x03,0x82,0x02,0x82, -0x03,0x82,0x03,0x82,0x04,0x82,0x02,0x82,0x03,0x82,0x03,0x82,0x04,0x82,0x02, -0x82,0x06,0x82,0x05,0x82,0x04,0x82,0x02,0x82,0x04,0x82,0x02,0x82,0x04,0x82, -0x02,0x82,0x04,0x82,0x02,0x82,0x04,0x82,0x08,0x82,0x03,0x82,0x08,0x82,0x0c, -0x82,0x05,0x84,0x11,0x82,0x0f,0x82,0x18,0x82,0x0e,0x82,0x02,0x82,0x0c,0x82, -0x1c,0x82,0x0b,0x82,0x7f,0x15,0x82,0x08,0x82,0x08,0x82,0x05,0x82,0x01,0x82, -0x01,0x82,0x19,0x82,0x06,0x82,0x02,0x82,0x06,0x82,0x01,0x82,0x02,0x82,0x01, -0x82,0x01,0x82,0x02,0x82,0x01,0x82,0x01,0x82,0x03,0x82,0x01,0x82,0x07,0x82, -0x08,0x82,0x08,0x82,0x3d,0x82,0x03,0x82,0x02,0x82,0x04,0x84,0x05,0x82,0x04, -0x82,0x02,0x82,0x04,0x82,0x06,0x83,0x03,0x82,0x08,0x82,0x04,0x81,0x09,0x82, -0x02,0x82,0x04,0x82,0x02,0x82,0x04,0x82,0x1a,0x82,0x10,0x82,0x06,0x82,0x04, -0x82,0x02,0x82,0x04,0x82,0x03,0x82,0x02,0x82,0x03,0x82,0x03,0x82,0x03,0x82, -0x04,0x82,0x02,0x82,0x04,0x82,0x02,0x82,0x08,0x82,0x08,0x82,0x04,0x82,0x02, -0x82,0x04,0x82,0x05,0x82,0x0a,0x82,0x03,0x82,0x03,0x82,0x03,0x82,0x08,0x83, -0x02,0x83,0x02,0x83,0x03,0x82,0x02,0x82,0x04,0x82,0x02,0x82,0x04,0x82,0x02, -0x82,0x04,0x82,0x02,0x82,0x04,0x82,0x02,0x82,0x04,0x82,0x05,0x82,0x05,0x82, -0x04,0x82,0x02,0x82,0x04,0x82,0x02,0x82,0x04,0x82,0x03,0x82,0x02,0x82,0x04, -0x82,0x02,0x82,0x09,0x82,0x03,0x82,0x08,0x82,0x0c,0x82,0x04,0x82,0x02,0x82, -0x11,0x82,0x0e,0x82,0x18,0x82,0x0e,0x82,0x02,0x82,0x0c,0x82,0x0b,0x82,0x0b, -0x82,0x02,0x82,0x0b,0x82,0x4d,0x82,0x45,0x82,0x08,0x82,0x08,0x82,0x05,0x82, -0x02,0x83,0x1a,0x82,0x07,0x81,0x02,0x81,0x07,0x82,0x01,0x82,0x02,0x82,0x01, -0x82,0x05,0x82,0x01,0x84,0x04,0x82,0x01,0x82,0x07,0x82,0x08,0x82,0x08,0x82, -0x06,0x82,0x02,0x82,0x06,0x82,0x28,0x82,0x04,0x82,0x02,0x82,0x03,0x82,0x01, -0x82,0x05,0x82,0x04,0x82,0x02,0x82,0x04,0x82,0x05,0x84,0x03,0x82,0x08,0x82, -0x0d,0x82,0x03,0x82,0x04,0x82,0x02,0x82,0x04,0x82,0x19,0x82,0x12,0x82,0x05, -0x82,0x04,0x82,0x02,0x82,0x02,0x84,0x03,0x82,0x02,0x82,0x03,0x82,0x03,0x82, -0x03,0x82,0x08,0x82,0x04,0x82,0x02,0x82,0x08,0x82,0x08,0x82,0x08,0x82,0x04, -0x82,0x05,0x82,0x0a,0x82,0x03,0x82,0x03,0x82,0x03,0x82,0x08,0x83,0x02,0x83, -0x02,0x84,0x02,0x82,0x02,0x82,0x04,0x82,0x02,0x82,0x04,0x82,0x02,0x82,0x04, -0x82,0x02,0x82,0x04,0x82,0x02,0x82,0x0b,0x82,0x05,0x82,0x04,0x82,0x02,0x82, -0x04,0x82,0x02,0x82,0x04,0x82,0x03,0x82,0x02,0x82,0x04,0x82,0x02,0x82,0x08, -0x82,0x04,0x82,0x09,0x82,0x0b,0x82,0x03,0x82,0x04,0x82,0x20,0x82,0x18,0x82, -0x0e,0x82,0x10,0x82,0x0b,0x82,0x0b,0x82,0x02,0x82,0x0b,0x82,0x4d,0x82,0x45, -0x82,0x08,0x82,0x08,0x82,0x26,0x82,0x10,0x88,0x01,0x82,0x01,0x82,0x06,0x83, -0x01,0x82,0x04,0x84,0x08,0x81,0x08,0x82,0x0a,0x82,0x05,0x82,0x02,0x82,0x06, -0x82,0x28,0x82,0x03,0x82,0x04,0x82,0x05,0x82,0x0b,0x82,0x08,0x82,0x04,0x82, -0x01,0x82,0x03,0x82,0x08,0x82,0x0d,0x82,0x03,0x82,0x04,0x82,0x02,0x82,0x04, -0x82,0x18,0x82,0x06,0x88,0x06,0x82,0x04,0x82,0x04,0x82,0x02,0x82,0x01,0x85, -0x02,0x82,0x04,0x82,0x02,0x82,0x03,0x82,0x03,0x82,0x08,0x82,0x04,0x82,0x02, -0x82,0x08,0x82,0x08,0x82,0x08,0x82,0x04,0x82,0x05,0x82,0x0a,0x82,0x03,0x82, -0x02,0x82,0x04,0x82,0x08,0x88,0x02,0x84,0x02,0x82,0x02,0x82,0x04,0x82,0x02, -0x82,0x04,0x82,0x02,0x82,0x04,0x82,0x02,0x82,0x04,0x82,0x02,0x82,0x0b,0x82, -0x05,0x82,0x04,0x82,0x03,0x82,0x02,0x82,0x03,0x82,0x04,0x82,0x04,0x84,0x06, -0x84,0x08,0x82,0x05,0x82,0x09,0x82,0x0b,0x82,0x2b,0x82,0x18,0x82,0x0e,0x82, -0x10,0x82,0x1c,0x82,0x0b,0x82,0x4d,0x82,0x45,0x82,0x08,0x82,0x08,0x82,0x26, -0x82,0x11,0x82,0x01,0x82,0x03,0x82,0x01,0x82,0x09,0x82,0x06,0x82,0x12,0x82, -0x0a,0x82,0x06,0x84,0x07,0x82,0x27,0x82,0x04,0x82,0x04,0x82,0x05,0x82,0x0b, -0x82,0x07,0x82,0x04,0x82,0x02,0x82,0x03,0x82,0x01,0x83,0x04,0x82,0x01,0x83, -0x08,0x82,0x05,0x82,0x02,0x82,0x03,0x82,0x04,0x82,0x05,0x83,0x07,0x83,0x05, -0x82,0x16,0x82,0x08,0x82,0x03,0x82,0x01,0x82,0x01,0x82,0x02,0x82,0x04,0x82, -0x02,0x82,0x02,0x82,0x04,0x82,0x08,0x82,0x04,0x82,0x02,0x82,0x08,0x82,0x08, -0x82,0x08,0x82,0x04,0x82,0x05,0x82,0x0a,0x82,0x03,0x82,0x02,0x82,0x04,0x82, -0x08,0x82,0x01,0x82,0x01,0x82,0x02,0x82,0x01,0x82,0x01,0x82,0x02,0x82,0x04, -0x82,0x02,0x82,0x04,0x82,0x02,0x82,0x04,0x82,0x02,0x82,0x04,0x82,0x03,0x82, -0x0a,0x82,0x05,0x82,0x04,0x82,0x03,0x82,0x02,0x82,0x03,0x82,0x01,0x82,0x01, -0x82,0x04,0x84,0x06,0x84,0x08,0x82,0x05,0x82,0x0a,0x82,0x0a,0x82,0x23,0x85, -0x03,0x82,0x01,0x83,0x06,0x85,0x05,0x83,0x01,0x82,0x04,0x84,0x04,0x86,0x05, -0x85,0x01,0x81,0x02,0x82,0x01,0x83,0x05,0x84,0x09,0x84,0x02,0x82,0x03,0x82, -0x06,0x82,0x05,0x81,0x01,0x82,0x01,0x82,0x03,0x82,0x01,0x83,0x06,0x84,0x04, -0x82,0x01,0x83,0x06,0x83,0x01,0x82,0x02,0x82,0x01,0x84,0x04,0x86,0x03,0x86, -0x04,0x82,0x04,0x82,0x02,0x82,0x04,0x82,0x02,0x82,0x04,0x82,0x02,0x82,0x04, -0x82,0x02,0x82,0x04,0x82,0x03,0x87,0x05,0x82,0x08,0x82,0x08,0x82,0x26,0x82, -0x11,0x82,0x01,0x82,0x04,0x86,0x07,0x82,0x05,0x83,0x12,0x82,0x0a,0x82,0x04, -0x88,0x02,0x88,0x0c,0x88,0x10,0x82,0x04,0x82,0x04,0x82,0x05,0x82,0x0a,0x82, -0x06,0x83,0x04,0x82,0x03,0x82,0x03,0x83,0x02,0x82,0x03,0x83,0x02,0x82,0x07, -0x82,0x06,0x84,0x05,0x82,0x02,0x83,0x05,0x83,0x07,0x83,0x04,0x82,0x18,0x82, -0x06,0x82,0x04,0x82,0x01,0x82,0x01,0x82,0x02,0x82,0x04,0x82,0x02,0x86,0x04, -0x82,0x08,0x82,0x04,0x82,0x02,0x86,0x04,0x86,0x04,0x82,0x02,0x84,0x02,0x88, -0x05,0x82,0x0a,0x82,0x03,0x85,0x05,0x82,0x08,0x82,0x01,0x82,0x01,0x82,0x02, -0x82,0x01,0x82,0x01,0x82,0x02,0x82,0x04,0x82,0x02,0x82,0x03,0x82,0x03,0x82, -0x04,0x82,0x02,0x82,0x03,0x82,0x05,0x84,0x07,0x82,0x05,0x82,0x04,0x82,0x03, -0x82,0x02,0x82,0x03,0x82,0x01,0x82,0x01,0x82,0x05,0x82,0x08,0x82,0x08,0x82, -0x06,0x82,0x0a,0x82,0x0a,0x82,0x22,0x82,0x03,0x82,0x02,0x83,0x02,0x82,0x04, -0x82,0x03,0x82,0x03,0x82,0x02,0x83,0x03,0x82,0x02,0x82,0x05,0x82,0x06,0x82, -0x03,0x83,0x02,0x83,0x02,0x82,0x06,0x82,0x0b,0x82,0x02,0x82,0x02,0x82,0x07, -0x82,0x05,0x88,0x02,0x83,0x02,0x82,0x04,0x82,0x02,0x82,0x03,0x83,0x02,0x82, -0x04,0x82,0x02,0x83,0x03,0x83,0x02,0x82,0x02,0x82,0x04,0x82,0x04,0x82,0x06, -0x82,0x04,0x82,0x02,0x82,0x04,0x82,0x02,0x82,0x04,0x82,0x03,0x82,0x02,0x82, -0x03,0x82,0x04,0x82,0x08,0x82,0x02,0x84,0x09,0x82,0x09,0x84,0x23,0x82,0x11, -0x82,0x01,0x82,0x06,0x82,0x01,0x82,0x05,0x82,0x05,0x82,0x01,0x82,0x11,0x82, -0x0a,0x82,0x06,0x84,0x07,0x82,0x26,0x82,0x05,0x82,0x04,0x82,0x05,0x82,0x08, -0x83,0x09,0x82,0x03,0x82,0x03,0x82,0x09,0x82,0x02,0x82,0x04,0x82,0x05,0x82, -0x06,0x82,0x02,0x82,0x05,0x83,0x01,0x82,0x17,0x82,0x16,0x82,0x06,0x82,0x05, -0x82,0x01,0x82,0x01,0x82,0x02,0x88,0x02,0x82,0x03,0x82,0x03,0x82,0x08,0x82, -0x04,0x82,0x02,0x82,0x08,0x82,0x08,0x82,0x04,0x82,0x02,0x82,0x04,0x82,0x05, -0x82,0x0a,0x82,0x03,0x82,0x02,0x82,0x04,0x82,0x08,0x82,0x01,0x82,0x01,0x82, -0x02,0x82,0x02,0x84,0x02,0x82,0x04,0x82,0x02,0x86,0x04,0x82,0x04,0x82,0x02, -0x86,0x09,0x82,0x06,0x82,0x05,0x82,0x04,0x82,0x04,0x84,0x04,0x82,0x01,0x82, -0x01,0x82,0x04,0x84,0x07,0x82,0x07,0x82,0x07,0x82,0x0b,0x82,0x09,0x82,0x27, -0x82,0x02,0x82,0x04,0x82,0x02,0x82,0x08,0x82,0x04,0x82,0x02,0x82,0x04,0x82, -0x04,0x82,0x06,0x82,0x03,0x82,0x03,0x82,0x04,0x82,0x05,0x82,0x0b,0x82,0x02, -0x82,0x01,0x82,0x08,0x82,0x05,0x82,0x01,0x82,0x01,0x82,0x02,0x82,0x04,0x82, -0x02,0x82,0x04,0x82,0x02,0x82,0x04,0x82,0x02,0x82,0x04,0x82,0x03,0x82,0x07, -0x82,0x0a,0x82,0x06,0x82,0x04,0x82,0x03,0x82,0x02,0x82,0x03,0x82,0x04,0x82, -0x04,0x84,0x04,0x82,0x04,0x82,0x07,0x82,0x06,0x82,0x08,0x82,0x08,0x82,0x26, -0x82,0x0f,0x88,0x05,0x82,0x01,0x82,0x05,0x82,0x05,0x82,0x02,0x82,0x01,0x82, -0x0d,0x82,0x0a,0x82,0x05,0x82,0x02,0x82,0x06,0x82,0x26,0x82,0x05,0x82,0x04, -0x82,0x05,0x82,0x07,0x82,0x0c,0x82,0x02,0x88,0x08,0x82,0x02,0x82,0x04,0x82, -0x05,0x82,0x05,0x82,0x04,0x82,0x08,0x82,0x18,0x82,0x14,0x82,0x07,0x82,0x05, -0x82,0x01,0x84,0x03,0x82,0x04,0x82,0x02,0x82,0x04,0x82,0x02,0x82,0x08,0x82, -0x04,0x82,0x02,0x82,0x08,0x82,0x08,0x82,0x04,0x82,0x02,0x82,0x04,0x82,0x05, -0x82,0x0a,0x82,0x03,0x82,0x02,0x82,0x04,0x82,0x08,0x82,0x01,0x82,0x01,0x82, -0x02,0x82,0x02,0x84,0x02,0x82,0x04,0x82,0x02,0x82,0x08,0x82,0x04,0x82,0x02, -0x82,0x02,0x82,0x0a,0x82,0x05,0x82,0x05,0x82,0x04,0x82,0x04,0x84,0x04,0x82, -0x01,0x82,0x01,0x82,0x04,0x84,0x07,0x82,0x07,0x82,0x07,0x82,0x0b,0x82,0x09, -0x82,0x22,0x87,0x02,0x82,0x04,0x82,0x02,0x82,0x08,0x82,0x04,0x82,0x02,0x88, -0x04,0x82,0x06,0x82,0x03,0x82,0x03,0x82,0x04,0x82,0x05,0x82,0x0b,0x82,0x02, -0x84,0x09,0x82,0x05,0x82,0x01,0x82,0x01,0x82,0x02,0x82,0x04,0x82,0x02,0x82, -0x04,0x82,0x02,0x82,0x04,0x82,0x02,0x82,0x04,0x82,0x03,0x82,0x08,0x86,0x05, -0x82,0x06,0x82,0x04,0x82,0x03,0x82,0x02,0x82,0x03,0x82,0x01,0x82,0x01,0x82, -0x05,0x82,0x05,0x82,0x04,0x82,0x06,0x82,0x07,0x82,0x08,0x82,0x08,0x82,0x26, -0x82,0x10,0x82,0x01,0x82,0x07,0x82,0x01,0x82,0x04,0x82,0x01,0x83,0x02,0x82, -0x03,0x83,0x0f,0x82,0x08,0x82,0x06,0x82,0x02,0x82,0x06,0x82,0x25,0x82,0x07, -0x82,0x02,0x82,0x06,0x82,0x06,0x82,0x07,0x82,0x04,0x82,0x07,0x82,0x09,0x82, -0x02,0x82,0x04,0x82,0x04,0x82,0x06,0x82,0x04,0x82,0x08,0x82,0x19,0x82,0x05, -0x88,0x05,0x82,0x08,0x82,0x05,0x82,0x02,0x82,0x04,0x82,0x04,0x82,0x02,0x82, -0x04,0x82,0x02,0x82,0x08,0x82,0x04,0x82,0x02,0x82,0x08,0x82,0x08,0x82,0x04, -0x82,0x02,0x82,0x04,0x82,0x05,0x82,0x05,0x82,0x03,0x82,0x03,0x82,0x03,0x82, -0x03,0x82,0x08,0x82,0x04,0x82,0x02,0x82,0x03,0x83,0x02,0x82,0x04,0x82,0x02, -0x82,0x08,0x82,0x01,0x82,0x01,0x82,0x02,0x82,0x03,0x82,0x09,0x82,0x05,0x82, -0x05,0x82,0x04,0x82,0x04,0x84,0x04,0x83,0x02,0x83,0x03,0x82,0x02,0x82,0x06, -0x82,0x06,0x82,0x08,0x82,0x0c,0x82,0x08,0x82,0x21,0x82,0x04,0x82,0x02,0x82, -0x04,0x82,0x02,0x82,0x08,0x82,0x04,0x82,0x02,0x82,0x0a,0x82,0x06,0x82,0x03, -0x82,0x03,0x82,0x04,0x82,0x05,0x82,0x0b,0x82,0x02,0x85,0x08,0x82,0x05,0x82, -0x01,0x82,0x01,0x82,0x02,0x82,0x04,0x82,0x02,0x82,0x04,0x82,0x02,0x82,0x04, -0x82,0x02,0x82,0x04,0x82,0x03,0x82,0x0d,0x82,0x04,0x82,0x06,0x82,0x04,0x82, -0x04,0x84,0x04,0x82,0x01,0x82,0x01,0x82,0x05,0x82,0x05,0x82,0x04,0x82,0x05, -0x82,0x08,0x82,0x08,0x82,0x08,0x82,0x38,0x82,0x01,0x82,0x04,0x82,0x01,0x82, -0x01,0x82,0x04,0x84,0x01,0x82,0x01,0x82,0x03,0x82,0x10,0x82,0x08,0x82,0x30, -0x83,0x06,0x82,0x07,0x82,0x02,0x82,0x06,0x82,0x05,0x82,0x08,0x82,0x04,0x82, -0x07,0x82,0x03,0x82,0x04,0x82,0x02,0x82,0x04,0x82,0x04,0x82,0x06,0x82,0x04, -0x82,0x03,0x81,0x04,0x82,0x1a,0x82,0x10,0x82,0x10,0x82,0x08,0x82,0x04,0x82, -0x02,0x82,0x04,0x82,0x02,0x82,0x04,0x82,0x02,0x82,0x04,0x82,0x02,0x82,0x08, -0x82,0x08,0x82,0x04,0x82,0x02,0x82,0x04,0x82,0x05,0x82,0x05,0x82,0x03,0x82, -0x03,0x82,0x03,0x82,0x03,0x82,0x08,0x82,0x04,0x82,0x02,0x82,0x03,0x83,0x02, -0x82,0x04,0x82,0x02,0x82,0x08,0x82,0x02,0x84,0x02,0x82,0x03,0x82,0x03,0x82, -0x04,0x82,0x05,0x82,0x05,0x82,0x04,0x82,0x05,0x82,0x05,0x83,0x02,0x83,0x03, -0x82,0x02,0x82,0x06,0x82,0x05,0x82,0x09,0x82,0x0c,0x82,0x08,0x82,0x21,0x82, -0x04,0x82,0x02,0x82,0x04,0x82,0x02,0x82,0x08,0x82,0x04,0x82,0x02,0x82,0x0a, -0x82,0x07,0x85,0x04,0x82,0x04,0x82,0x05,0x82,0x0b,0x82,0x02,0x82,0x02,0x82, -0x07,0x82,0x05,0x82,0x01,0x82,0x01,0x82,0x02,0x82,0x04,0x82,0x02,0x82,0x04, -0x82,0x02,0x82,0x04,0x82,0x02,0x82,0x04,0x82,0x03,0x82,0x0d,0x82,0x04,0x82, -0x06,0x82,0x04,0x82,0x04,0x84,0x04,0x82,0x01,0x82,0x01,0x82,0x04,0x84,0x04, -0x82,0x04,0x82,0x04,0x82,0x09,0x82,0x08,0x82,0x08,0x82,0x26,0x82,0x10,0x82, -0x01,0x82,0x05,0x86,0x04,0x82,0x01,0x82,0x01,0x82,0x01,0x83,0x01,0x84,0x10, -0x82,0x06,0x82,0x1d,0x83,0x11,0x83,0x05,0x82,0x09,0x84,0x07,0x82,0x05,0x82, -0x09,0x82,0x02,0x82,0x08,0x82,0x04,0x82,0x02,0x82,0x04,0x82,0x02,0x82,0x04, -0x82,0x08,0x82,0x02,0x82,0x04,0x82,0x02,0x82,0x06,0x83,0x07,0x83,0x09,0x82, -0x0e,0x82,0x0a,0x82,0x06,0x82,0x03,0x82,0x02,0x82,0x04,0x82,0x02,0x82,0x03, -0x82,0x04,0x82,0x02,0x82,0x03,0x82,0x03,0x82,0x03,0x82,0x08,0x82,0x09,0x82, -0x02,0x83,0x02,0x82,0x04,0x82,0x05,0x82,0x06,0x82,0x01,0x82,0x04,0x82,0x04, -0x82,0x02,0x82,0x08,0x82,0x04,0x82,0x02,0x82,0x04,0x82,0x03,0x82,0x02,0x82, -0x03,0x82,0x09,0x82,0x02,0x82,0x03,0x82,0x04,0x82,0x03,0x82,0x02,0x82,0x06, -0x82,0x06,0x82,0x02,0x82,0x06,0x82,0x05,0x82,0x04,0x82,0x02,0x82,0x04,0x82, -0x05,0x82,0x05,0x82,0x09,0x82,0x0d,0x82,0x07,0x82,0x21,0x82,0x04,0x82,0x02, -0x83,0x02,0x82,0x04,0x82,0x03,0x82,0x03,0x82,0x02,0x83,0x03,0x82,0x03,0x82, -0x04,0x82,0x06,0x82,0x08,0x82,0x04,0x82,0x05,0x82,0x0b,0x82,0x02,0x82,0x03, -0x82,0x06,0x82,0x05,0x82,0x01,0x82,0x01,0x82,0x02,0x82,0x04,0x82,0x03,0x82, -0x02,0x82,0x03,0x83,0x02,0x82,0x04,0x82,0x02,0x83,0x03,0x82,0x07,0x82,0x04, -0x82,0x04,0x82,0x02,0x82,0x03,0x82,0x02,0x83,0x05,0x82,0x05,0x88,0x03,0x82, -0x02,0x82,0x04,0x82,0x02,0x83,0x03,0x82,0x0a,0x82,0x08,0x82,0x08,0x82,0x26, -0x82,0x1c,0x82,0x06,0x82,0x02,0x83,0x03,0x84,0x02,0x82,0x10,0x82,0x04,0x82, -0x1e,0x83,0x11,0x83,0x05,0x82,0x0a,0x82,0x05,0x88,0x02,0x88,0x04,0x84,0x09, -0x82,0x05,0x84,0x06,0x84,0x05,0x82,0x09,0x84,0x06,0x84,0x07,0x83,0x07,0x83, -0x0a,0x81,0x0e,0x81,0x0b,0x82,0x07,0x85,0x03,0x82,0x04,0x82,0x02,0x86,0x06, -0x84,0x04,0x86,0x04,0x88,0x02,0x82,0x0a,0x84,0x01,0x81,0x02,0x82,0x04,0x82, -0x02,0x88,0x04,0x83,0x05,0x82,0x04,0x82,0x02,0x88,0x02,0x82,0x04,0x82,0x02, -0x82,0x04,0x82,0x04,0x84,0x04,0x82,0x0a,0x85,0x03,0x82,0x04,0x82,0x04,0x84, -0x07,0x82,0x07,0x84,0x07,0x82,0x05,0x82,0x04,0x82,0x02,0x82,0x04,0x82,0x05, -0x82,0x05,0x88,0x03,0x86,0x09,0x82,0x03,0x86,0x22,0x85,0x01,0x81,0x02,0x82, -0x01,0x83,0x06,0x85,0x05,0x83,0x01,0x82,0x04,0x85,0x05,0x82,0x07,0x86,0x03, -0x82,0x04,0x82,0x02,0x88,0x08,0x82,0x02,0x82,0x04,0x82,0x02,0x88,0x02,0x82, -0x01,0x82,0x01,0x82,0x02,0x82,0x04,0x82,0x04,0x84,0x04,0x82,0x01,0x83,0x06, -0x83,0x01,0x82,0x03,0x82,0x08,0x86,0x06,0x84,0x05,0x83,0x01,0x82,0x05,0x82, -0x06,0x82,0x02,0x82,0x03,0x82,0x04,0x82,0x04,0x83,0x01,0x82,0x03,0x87,0x06, -0x84,0x05,0x82,0x05,0x84,0x7f,0x15,0x83,0x7f,0x14,0x83,0x7f,0x5e,0x82,0x7f, -0x05,0x89,0x47,0x82,0x04,0x82,0x17,0x82,0x03,0x82,0x34,0x82,0x0e,0x82,0x4e, -0x82,0x7f,0x7f,0x7f,0x7f,0x7f,0x7f,0x0a,0x82,0x04,0x82,0x17,0x82,0x03,0x82, -0x34,0x82,0x0e,0x82,0x48,0x82,0x04,0x82,0x7f,0x7f,0x7f,0x7f,0x7f,0x7f,0x0a, -0x82,0x04,0x82,0x17,0x82,0x03,0x82,0x34,0x82,0x0e,0x82,0x49,0x82,0x02,0x82, -0x7f,0x7f,0x7f,0x7f,0x7f,0x7f,0x0c,0x86,0x19,0x85,0x35,0x82,0x0e,0x82,0x4a, -0x84,0x3f, -0x00, - } -}; diff --git a/minui/graphics.cpp b/minui/graphics.cpp index 56f471bce2..202ce71fde 100644 --- a/minui/graphics.cpp +++ b/minui/graphics.cpp @@ -23,7 +23,6 @@ #include -#include "font_10x18.h" #include "graphics_adf.h" #include "graphics_drm.h" #include "graphics_fbdev.h" @@ -313,42 +312,16 @@ int gr_init_font(const char* name, GRFont** dest) { return 0; } -static void gr_init_font(void) { - int res = gr_init_font("font", &gr_font); - if (res == 0) { - return; - } - - printf("failed to read font: res=%d\n", res); - - // fall back to the compiled-in font. - gr_font = static_cast(calloc(1, sizeof(*gr_font))); - gr_font->texture = static_cast(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 = static_cast(malloc(font.width * font.height)); - gr_font->texture->data = 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; -} - void gr_flip() { gr_draw = gr_backend->Flip(); } int gr_init() { - gr_init_font(); + int ret = gr_init_font("font", &gr_font); + if (ret != 0) { + printf("Failed to init font: %d\n", ret); + return -1; + } auto backend = std::unique_ptr{ std::make_unique() }; gr_draw = backend->Init(); diff --git a/minui/mkfont.c b/minui/mkfont.c deleted file mode 100644 index 61a5edeb29..0000000000 --- a/minui/mkfont.c +++ /dev/null @@ -1,54 +0,0 @@ -#include -#include - -int main(int argc, char *argv) -{ - unsigned n; - unsigned char *x; - unsigned m; - unsigned run_val; - unsigned run_count; - - n = gimp_image.width * gimp_image.height; - m = 0; - x = gimp_image.pixel_data; - - printf("struct {\n"); - printf(" unsigned width;\n"); - printf(" unsigned height;\n"); - printf(" unsigned cwidth;\n"); - printf(" unsigned cheight;\n"); - printf(" unsigned char rundata[];\n"); - printf("} font = {\n"); - printf(" .width = %d,\n .height = %d,\n .cwidth = %d,\n .cheight = %d,\n", gimp_image.width, gimp_image.height, - gimp_image.width / 96, gimp_image.height); - printf(" .rundata = {\n"); - - run_val = (*x ? 0 : 255); - run_count = 1; - n--; - x+=3; - - while(n-- > 0) { - unsigned val = (*x ? 0 : 255); - x+=3; - if((val == run_val) && (run_count < 127)) { - run_count++; - } else { -eject: - printf("0x%02x,",run_count | (run_val ? 0x80 : 0x00)); - run_val = val; - run_count = 1; - m += 5; - if(m >= 75) { - printf("\n"); - m = 0; - } - } - } - printf("0x%02x,",run_count | (run_val ? 0x80 : 0x00)); - printf("\n0x00,"); - printf("\n"); - printf(" }\n};\n"); - return 0; -} From 051b968d7b26e3a53ab82b9f3119783ef86b63b8 Mon Sep 17 00:00:00 2001 From: Jiachen Zhao Date: Fri, 30 Mar 2018 14:04:51 -0700 Subject: [PATCH 017/560] Create folder for SystemUpdate sample app. Test: no functional changes. Change-Id: I2e8c654086ca49063c59ae0529e190323c84d6ad --- Android.mk | 1 + sample_updater/Android.mk | 25 +++++++++++++++++++++++++ sample_updater/AndroidManifest.xml | 21 +++++++++++++++++++++ sample_updater/README.md | 1 + 4 files changed, 48 insertions(+) create mode 100644 sample_updater/Android.mk create mode 100644 sample_updater/AndroidManifest.xml create mode 100644 sample_updater/README.md diff --git a/Android.mk b/Android.mk index 5dca32a453..98d1f0cc62 100644 --- a/Android.mk +++ b/Android.mk @@ -276,6 +276,7 @@ include $(BUILD_STATIC_LIBRARY) include \ $(LOCAL_PATH)/boot_control/Android.mk \ $(LOCAL_PATH)/minui/Android.mk \ + $(LOCAL_PATH)/sample_updater/Android.mk \ $(LOCAL_PATH)/tests/Android.mk \ $(LOCAL_PATH)/tools/Android.mk \ $(LOCAL_PATH)/updater/Android.mk \ diff --git a/sample_updater/Android.mk b/sample_updater/Android.mk new file mode 100644 index 0000000000..d6764bd01b --- /dev/null +++ b/sample_updater/Android.mk @@ -0,0 +1,25 @@ +# +# Copyright (C) 2018 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_PACKAGE_NAME := SystemUpdateApp +LOCAL_SDK_VERSION := system_current +LOCAL_MODULE_TAGS := optional + +include $(BUILD_PACKAGE) diff --git a/sample_updater/AndroidManifest.xml b/sample_updater/AndroidManifest.xml new file mode 100644 index 0000000000..ccee107bb5 --- /dev/null +++ b/sample_updater/AndroidManifest.xml @@ -0,0 +1,21 @@ + + + + + diff --git a/sample_updater/README.md b/sample_updater/README.md new file mode 100644 index 0000000000..a06c52d4bc --- /dev/null +++ b/sample_updater/README.md @@ -0,0 +1 @@ +# System update sample app. From f39362e1848cde65fbfd6b80a53a33e5d0672cc5 Mon Sep 17 00:00:00 2001 From: Jiachen Zhao Date: Fri, 30 Mar 2018 15:24:26 -0700 Subject: [PATCH 018/560] Create the SystemUpdate activity class. Test: tested sample app with sideloading. Change-Id: I0d1661cea57c706b4981a4cda9f9b69fdeb69292 --- sample_updater/Android.mk | 2 + sample_updater/AndroidManifest.xml | 18 +++-- sample_updater/res/layout/activity_main.xml | 20 ++++++ .../update/ui/SystemUpdateActivity.java | 68 +++++++++++++++++++ 4 files changed, 104 insertions(+), 4 deletions(-) create mode 100644 sample_updater/res/layout/activity_main.xml create mode 100644 sample_updater/src/com/android/update/ui/SystemUpdateActivity.java diff --git a/sample_updater/Android.mk b/sample_updater/Android.mk index d6764bd01b..2b0fcbeec2 100644 --- a/sample_updater/Android.mk +++ b/sample_updater/Android.mk @@ -22,4 +22,6 @@ LOCAL_PACKAGE_NAME := SystemUpdateApp LOCAL_SDK_VERSION := system_current LOCAL_MODULE_TAGS := optional +LOCAL_SRC_FILES := $(call all-java-files-under, src) + include $(BUILD_PACKAGE) diff --git a/sample_updater/AndroidManifest.xml b/sample_updater/AndroidManifest.xml index ccee107bb5..66414b5d31 100644 --- a/sample_updater/AndroidManifest.xml +++ b/sample_updater/AndroidManifest.xml @@ -14,8 +14,18 @@ limitations under the License. --> - + + + + + + + + + + + + diff --git a/sample_updater/res/layout/activity_main.xml b/sample_updater/res/layout/activity_main.xml new file mode 100644 index 0000000000..bd7d686771 --- /dev/null +++ b/sample_updater/res/layout/activity_main.xml @@ -0,0 +1,20 @@ + + + + + diff --git a/sample_updater/src/com/android/update/ui/SystemUpdateActivity.java b/sample_updater/src/com/android/update/ui/SystemUpdateActivity.java new file mode 100644 index 0000000000..e57b1673c2 --- /dev/null +++ b/sample_updater/src/com/android/update/ui/SystemUpdateActivity.java @@ -0,0 +1,68 @@ +/* + * Copyright (C) 2018 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. + */ + +package com.android.update.ui; + +import android.app.Activity; +import android.os.UpdateEngine; +import android.os.UpdateEngineCallback; + +/** Main update activity. */ +public class SystemUpdateActivity extends Activity { + + private UpdateEngine updateEngine; + private UpdateEngineCallbackImpl updateEngineCallbackImpl = new UpdateEngineCallbackImpl(this); + + @Override + public void onResume() { + super.onResume(); + updateEngine = new UpdateEngine(); + updateEngine.bind(updateEngineCallbackImpl); + } + + @Override + public void onPause() { + updateEngine.unbind(); + super.onPause(); + } + + void onStatusUpdate(int i, float v) { + // Handle update engine status update + } + + void onPayloadApplicationComplete(int i) { + // Handle apply payload completion + } + + private static class UpdateEngineCallbackImpl extends UpdateEngineCallback { + + private final SystemUpdateActivity activity; + + public UpdateEngineCallbackImpl(SystemUpdateActivity activity) { + this.activity = activity; + } + + @Override + public void onStatusUpdate(int i, float v) { + activity.onStatusUpdate(i, v); + } + + @Override + public void onPayloadApplicationComplete(int i) { + activity.onPayloadApplicationComplete(i); + } + } +} From a6f49bd9520e02d84026e31a53a6c98db59d91a3 Mon Sep 17 00:00:00 2001 From: Tianjie Xu Date: Mon, 26 Mar 2018 14:32:11 -0700 Subject: [PATCH 019/560] Do not skip the update installation if it's a retry We used to skip the update installation if the battery doesn't reach the threshold or the boot reason is in blacklist. Afterwards, we just cleared the BCB and reboot/shutdown the device. However, this leaves the device in a brick state if the update is a retry; because a subsequent reboot won't resume the update after the BCB gets cleared. This CL skips the battery and bootreason check on retries. An alternative solution is to conduct the check and shutdown the device without clearing the BCB on INSTALL_SKIPPED; but users who don't have context may reboot the device manually anyway. Bug: 65288661 Test: Trigger the INSTALL_SKIPPED with insufficient battery, and check the behavior with retry_count. Change-Id: I0bd8ae9cee0e35aeeec3df469a78cec9ee1b8db8 --- recovery.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/recovery.cpp b/recovery.cpp index 6bd2914633..910cd8296f 100644 --- a/recovery.cpp +++ b/recovery.cpp @@ -1537,14 +1537,14 @@ int main(int argc, char **argv) { // to log the update attempt since update_package is non-NULL. modified_flash = true; - if (!is_battery_ok()) { + if (retry_count == 0 && !is_battery_ok()) { ui->Print("battery capacity is not enough for installing package, needed is %d%%\n", BATTERY_OK_PERCENTAGE); // Log the error code to last_install when installation skips due to // low battery. log_failure_code(kLowBattery, update_package); status = INSTALL_SKIPPED; - } else if (bootreason_in_blacklist()) { + } else if (retry_count == 0 && 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); From 9d91e898317fe19051d486fb76271a89603b5de8 Mon Sep 17 00:00:00 2001 From: Tao Bao Date: Fri, 30 Mar 2018 20:43:45 -0700 Subject: [PATCH 020/560] tests: Pick up testdata with LOCAL_TEST_DATA. Also push the testdata in AndroidTest.xml for `atest`. LOCAL_TEST_DATA was added in commit [1], which ships testdata next to native tests. With this CL, 1) manually sync-ing via `adb sync` and running tests with `adb shell` keep working. 2) both of 32- and 64-bit recovery_{unit,component}_test now work in APCT continuous tests. Note that 64-bit tests were failing previously in APCT, due to missing testdata. 3) `atest recovery_unit_test` works, as the testdata gets pushed to /data/local/tmp. [1] commit d07ba4e2a625a8f000d042c1953adb612bccbbe2 in https://android-review.googlesource.com/c/platform/build/+/312567. Bug: 77320514 Test: Build recovery_{unit,component,manual}_test. Setup the test via `adb sync data`. Run both of 32- and 64-bit versions on device. Test: Build and run recovery_host_test on host, for both of 32- and 64-bit versions. Test: `atest recovery_component_test` Change-Id: Ie54037726043a3a659a80030b83db1f8c1de318d --- tests/Android.mk | 50 ++++++---------------------------- tests/AndroidTest.xml | 10 ++++--- tests/common/test_constants.h | 15 ++++------ tests/manual/recovery_test.cpp | 12 ++++---- 4 files changed, 25 insertions(+), 62 deletions(-) diff --git a/tests/Android.mk b/tests/Android.mk index 9a71371faf..58cefd2267 100644 --- a/tests/Android.mk +++ b/tests/Android.mk @@ -45,6 +45,8 @@ LOCAL_SRC_FILES := \ LOCAL_C_INCLUDES := bootable/recovery LOCAL_SHARED_LIBRARIES := liblog +LOCAL_TEST_DATA := \ + $(call find-test-data-in-subdirs, $(LOCAL_PATH), "*", testdata) include $(BUILD_NATIVE_TEST) # Manual tests @@ -61,25 +63,8 @@ LOCAL_SHARED_LIBRARIES := \ liblog \ libpng -resource_files := $(call find-files-in-subdirs, bootable/recovery, \ - "*_text.png", \ - res-mdpi/images \ - res-hdpi/images \ - res-xhdpi/images \ - res-xxhdpi/images \ - res-xxxhdpi/images \ - ) - -# The resource image files that will go to $OUT/data/nativetest/recovery. -testimage_out_path := $(TARGET_OUT_DATA)/nativetest/recovery -GEN := $(addprefix $(testimage_out_path)/, $(resource_files)) - -$(GEN): PRIVATE_PATH := $(LOCAL_PATH) -$(GEN): PRIVATE_CUSTOM_TOOL = cp $< $@ -$(GEN): $(testimage_out_path)/% : bootable/recovery/% - $(transform-generated-source) -LOCAL_GENERATED_SOURCES += $(GEN) - +LOCAL_TEST_DATA := \ + $(call find-test-data-in-subdirs, bootable/recovery, "*_text.png", res-*) include $(BUILD_NATIVE_TEST) # Component tests @@ -171,29 +156,8 @@ LOCAL_STATIC_LIBRARIES := \ libBionicGtestMain \ $(tune2fs_static_libraries) -testdata_files := $(call find-subdir-files, testdata/*) - -# The testdata files that will go to $OUT/data/nativetest/recovery. -testdata_out_path := $(TARGET_OUT_DATA)/nativetest/recovery -GEN := $(addprefix $(testdata_out_path)/, $(testdata_files)) -$(GEN): PRIVATE_PATH := $(LOCAL_PATH) -$(GEN): PRIVATE_CUSTOM_TOOL = cp $< $@ -$(GEN): $(testdata_out_path)/% : $(LOCAL_PATH)/% - $(transform-generated-source) -LOCAL_GENERATED_SOURCES += $(GEN) - -# A copy of the testdata to be packed into continuous_native_tests.zip. -testdata_continuous_zip_prefix := \ - $(call intermediates-dir-for,PACKAGING,recovery_component_test)/DATA -testdata_continuous_zip_path := $(testdata_continuous_zip_prefix)/nativetest/recovery -GEN := $(addprefix $(testdata_continuous_zip_path)/, $(testdata_files)) -$(GEN): PRIVATE_PATH := $(LOCAL_PATH) -$(GEN): PRIVATE_CUSTOM_TOOL = cp $< $@ -$(GEN): $(testdata_continuous_zip_path)/% : $(LOCAL_PATH)/% - $(transform-generated-source) -LOCAL_GENERATED_SOURCES += $(GEN) -LOCAL_PICKUP_FILES := $(testdata_continuous_zip_prefix) - +LOCAL_TEST_DATA := \ + $(call find-test-data-in-subdirs, $(LOCAL_PATH), "*", testdata) include $(BUILD_NATIVE_TEST) # Host tests @@ -222,4 +186,6 @@ LOCAL_STATIC_LIBRARIES := \ libBionicGtestMain LOCAL_SHARED_LIBRARIES := \ liblog +LOCAL_TEST_DATA := \ + $(call find-test-data-in-subdirs, $(LOCAL_PATH), "*", testdata) include $(BUILD_HOST_NATIVE_TEST) diff --git a/tests/AndroidTest.xml b/tests/AndroidTest.xml index 3999aa57d6..6b86085aae 100644 --- a/tests/AndroidTest.xml +++ b/tests/AndroidTest.xml @@ -16,16 +16,18 @@ diff --git a/tests/common/test_constants.h b/tests/common/test_constants.h index 514818e0aa..b6c27a754b 100644 --- a/tests/common/test_constants.h +++ b/tests/common/test_constants.h @@ -17,10 +17,10 @@ #ifndef _OTA_TEST_CONSTANTS_H #define _OTA_TEST_CONSTANTS_H -#include - #include +#include + // Zip entries in ziptest_valid.zip. static const std::string kATxtContents("abcdefghabcdefgh\n"); static const std::string kBTxtContents("abcdefgh\n"); @@ -32,14 +32,9 @@ static const std::string kATxtSha1Sum("32c96a03dc8cd20097940f351bca6261ee5a1643" // echo -n -e "abcdefgh\n" | sha1sum static const std::string kBTxtSha1Sum("e414af7161c9554089f4106d6f1797ef14a73666"); -static std::string from_testdata_base(const std::string& fname) { -#ifdef __ANDROID__ - static std::string data_root = getenv("ANDROID_DATA"); -#else - static std::string data_root = std::string(getenv("ANDROID_PRODUCT_OUT")) + "/data"; -#endif - - return data_root + "/nativetest/recovery/testdata/" + fname; +[[maybe_unused]] static std::string from_testdata_base(const std::string& fname) { + static std::string exec_dir = android::base::GetExecutableDirectory(); + return exec_dir + "/testdata/" + fname; } #endif // _OTA_TEST_CONSTANTS_H diff --git a/tests/manual/recovery_test.cpp b/tests/manual/recovery_test.cpp index 64e3b59e65..224ed5b683 100644 --- a/tests/manual/recovery_test.cpp +++ b/tests/manual/recovery_test.cpp @@ -34,7 +34,6 @@ static const std::string myFilename = "/data/misc/recovery/inject.txt"; static const std::string myContent = "Hello World\nWelcome to my recovery\n"; static const std::string kLocale = "zu"; -static const std::string kResourceTestDir = "/data/nativetest/recovery/"; // Failure is expected on systems that do not deliver either the // recovery-persist or recovery-refresh executables. Tests also require @@ -108,19 +107,20 @@ static int png_filter(const dirent* de) { return 1; } -// Find out all png files to test under /data/nativetest/recovery/. +// Find out all the PNG files to test, which stay under the same dir with the executable. static std::vector add_files() { + std::string exec_dir = android::base::GetExecutableDirectory(); std::vector files; - for (const std::string& str : image_dir) { - std::string dir_path = kResourceTestDir + str; + for (const std::string& image : image_dir) { + std::string dir_path = exec_dir + "/" + image; dirent** namelist; int n = scandir(dir_path.c_str(), &namelist, png_filter, alphasort); if (n == -1) { - printf("Failed to scan dir %s: %s\n", kResourceTestDir.c_str(), strerror(errno)); + printf("Failed to scan dir %s: %s\n", exec_dir.c_str(), strerror(errno)); return files; } if (n == 0) { - printf("No file is added for test in %s\n", kResourceTestDir.c_str()); + printf("No file is added for test in %s\n", exec_dir.c_str()); } while (n--) { From f139bb4c56ed7b8afa4f3f0382807ba7109461ad Mon Sep 17 00:00:00 2001 From: Jaegeuk Kim Date: Mon, 2 Apr 2018 13:37:35 -0700 Subject: [PATCH 021/560] mkfs.f2fs: specify sector size for target image size The total sectors that we want to format is used in different meanings from various users. This notifies its size based on 4096 bytes explicitly. Bug: 76407663 Change-Id: I3392646648264ad1ca78e4b87240edc9385a0cc4 Reported-by: katao@xiaomi.com Signed-off-by: Jaegeuk Kim --- roots.cpp | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/roots.cpp b/roots.cpp index 9ff5186c1b..8907bbd9b8 100644 --- a/roots.cpp +++ b/roots.cpp @@ -324,19 +324,34 @@ int format_volume(const char* volume, const char* directory) { } // Has to be f2fs because we checked earlier. + static constexpr int kSectorSize = 4096; std::string cmd("/sbin/mkfs.f2fs"); - std::vector make_f2fs_cmd = { cmd, "-d1", "-f", "-O", - "encrypt", "-O", "quota", v->blk_device }; - if (length >= 512) { - make_f2fs_cmd.push_back(std::to_string(length / 512)); + // clang-format off + std::vector make_f2fs_cmd = { + cmd, + "-d1", + "-f", + "-O", "encrypt", + "-O", "quota", + "-w", std::to_string(kSectorSize), + v->blk_device, + }; + // clang-format on + if (length >= kSectorSize) { + make_f2fs_cmd.push_back(std::to_string(length / kSectorSize)); } int result = exec_cmd(make_f2fs_cmd); if (result == 0 && directory != nullptr) { cmd = "/sbin/sload.f2fs"; + // clang-format off std::vector sload_f2fs_cmd = { - cmd, "-f", directory, "-t", volume, v->blk_device, + cmd, + "-f", directory, + "-t", volume, + v->blk_device, }; + // clang-format on result = exec_cmd(sload_f2fs_cmd); } if (result != 0) { From fc66f3fbc6cf7060fe23eea9db0712e7a0436709 Mon Sep 17 00:00:00 2001 From: Jaegeuk Kim Date: Wed, 4 Apr 2018 21:14:01 -0700 Subject: [PATCH 022/560] install: mkfs.f2fs: specify sector size for target image size The total sectors that we want to format is used in different meanings from various users. This notifies its size based on 512 bytes explicitly. Bug: 76407663 Change-Id: I20687b40a1733d3b459a45f8b64a338c37a7bc95 Signed-off-by: Jaegeuk Kim --- updater/install.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/updater/install.cpp b/updater/install.cpp index 2b6c20fe37..6732ab897f 100644 --- a/updater/install.cpp +++ b/updater/install.cpp @@ -548,6 +548,8 @@ Value* FormatFn(const char* name, State* state, const std::vector Date: Wed, 11 Apr 2018 14:38:09 -0700 Subject: [PATCH 023/560] Remove the old log files if cache space is insufficient for OTA We set the limit of the max stash size to 80% of cache size. But the cache space can still be insufficient for the update if the log files occupy a large chunk of /cache. So remove the old logs for now to make room for the update. Bug: 77528881 Test: unit tests pass Change-Id: Ia8bcb0ace11f8164ad9290bfb360e08e31d282cb --- applypatch/applypatch.cpp | 14 +- applypatch/freecache.cpp | 168 ++++++++++++++------- applypatch/include/applypatch/applypatch.h | 6 +- otautil/cache_location.cpp | 4 +- otautil/include/otautil/cache_location.h | 10 ++ tests/component/applypatch_test.cpp | 129 ++++++++++++++++ 6 files changed, 271 insertions(+), 60 deletions(-) diff --git a/applypatch/applypatch.cpp b/applypatch/applypatch.cpp index 7645a40053..6f6c187bef 100644 --- a/applypatch/applypatch.cpp +++ b/applypatch/applypatch.cpp @@ -436,13 +436,13 @@ static size_t FileSink(const unsigned char* data, size_t len, int fd) { // Return the amount of free space (in bytes) on the filesystem // containing filename. filename must exist. Return -1 on error. -size_t FreeSpaceForFile(const char* filename) { - struct statfs sf; - if (statfs(filename, &sf) != 0) { - printf("failed to statfs %s: %s\n", filename, strerror(errno)); - return -1; - } - return sf.f_bsize * sf.f_bavail; +size_t FreeSpaceForFile(const std::string& filename) { + struct statfs sf; + if (statfs(filename.c_str(), &sf) != 0) { + printf("failed to statfs %s: %s\n", filename.c_str(), strerror(errno)); + return -1; + } + return sf.f_bsize * sf.f_bavail; } int CacheSizeCheck(size_t bytes) { diff --git a/applypatch/freecache.cpp b/applypatch/freecache.cpp index ea364d8e63..cfab0f6db9 100644 --- a/applypatch/freecache.cpp +++ b/applypatch/freecache.cpp @@ -14,7 +14,10 @@ * limitations under the License. */ +#include +#include #include +#include #include #include #include @@ -22,20 +25,22 @@ #include #include #include -#include -#include +#include +#include #include #include #include +#include #include #include +#include #include "applypatch/applypatch.h" #include "otautil/cache_location.h" -static int EliminateOpenFiles(std::set* files) { +static int EliminateOpenFiles(const std::string& dirname, std::set* files) { std::unique_ptr d(opendir("/proc"), closedir); if (!d) { printf("error opening /proc: %s\n", strerror(errno)); @@ -62,7 +67,7 @@ static int EliminateOpenFiles(std::set* files) { int count = readlink(fd_path.c_str(), link, sizeof(link)-1); if (count >= 0) { link[count] = '\0'; - if (strncmp(link, "/cache/", 7) == 0) { + if (android::base::StartsWith(link, dirname)) { if (files->erase(link) > 0) { printf("%s is open by %s\n", link, de->d_name); } @@ -73,77 +78,138 @@ static int EliminateOpenFiles(std::set* files) { return 0; } -static std::set FindExpendableFiles() { +static std::vector FindExpendableFiles( + const std::string& dirname, const std::function& name_filter) { std::set files; - // We're allowed to delete unopened regular files in any of these - // directories. - const char* dirs[2] = {"/cache", "/cache/recovery/otatest"}; - - for (size_t i = 0; i < sizeof(dirs)/sizeof(dirs[0]); ++i) { - std::unique_ptr d(opendir(dirs[i]), closedir); - if (!d) { - printf("error opening %s: %s\n", dirs[i], strerror(errno)); + + std::unique_ptr d(opendir(dirname.c_str()), closedir); + if (!d) { + printf("error opening %s: %s\n", dirname.c_str(), strerror(errno)); + return {}; + } + + // Look for regular files in the directory (not in any subdirectories). + struct dirent* de; + while ((de = readdir(d.get())) != 0) { + std::string path = dirname + "/" + de->d_name; + + // We can't delete cache_temp_source; if it's there we might have restarted during + // installation and could be depending on it to be there. + if (path == CacheLocation::location().cache_temp_source()) { continue; } - // Look for regular files in the directory (not in any subdirectories). - struct dirent* de; - while ((de = readdir(d.get())) != 0) { - std::string path = std::string(dirs[i]) + "/" + de->d_name; - - // We can't delete cache_temp_source; if it's there we might have restarted during - // installation and could be depending on it to be there. - if (path == CacheLocation::location().cache_temp_source()) { - continue; - } + // Do not delete the file if it doesn't have the expected format. + if (name_filter != nullptr && !name_filter(de->d_name)) { + continue; + } - struct stat st; - if (stat(path.c_str(), &st) == 0 && S_ISREG(st.st_mode)) { - files.insert(path); - } + struct stat st; + if (stat(path.c_str(), &st) == 0 && S_ISREG(st.st_mode)) { + files.insert(path); } } - printf("%zu regular files in deletable directories\n", files.size()); - if (EliminateOpenFiles(&files) < 0) { - return std::set(); + printf("%zu regular files in deletable directory\n", files.size()); + if (EliminateOpenFiles(dirname, &files) < 0) { + return {}; + } + + return std::vector(files.begin(), files.end()); +} + +// Parses the index of given log file, e.g. 3 for last_log.3; returns max number if the log name +// doesn't have the expected format so that we'll delete these ones first. +static unsigned int GetLogIndex(const std::string& log_name) { + if (log_name == "last_log" || log_name == "last_kmsg") { + return 0; + } + + unsigned int index; + if (sscanf(log_name.c_str(), "last_log.%u", &index) == 1 || + sscanf(log_name.c_str(), "last_kmsg.%u", &index) == 1) { + return index; } - return files; + + return std::numeric_limits::max(); } int MakeFreeSpaceOnCache(size_t bytes_needed) { #ifndef __ANDROID__ // TODO (xunchang) implement a heuristic cache size check during host simulation. - printf("Skip making (%zu) bytes free space on cache; program is running on host\n", bytes_needed); + printf("Skip making (%zu) bytes free space on /cache; program is running on host\n", + bytes_needed); return 0; #endif - size_t free_now = FreeSpaceForFile("/cache"); - printf("%zu bytes free on /cache (%zu needed)\n", free_now, bytes_needed); + std::vector dirs = { "/cache", CacheLocation::location().cache_log_directory() }; + for (const auto& dirname : dirs) { + if (RemoveFilesInDirectory(bytes_needed, dirname, FreeSpaceForFile)) { + return 0; + } + } - if (free_now >= bytes_needed) { - return 0; + return -1; +} + +bool RemoveFilesInDirectory(size_t bytes_needed, const std::string& dirname, + const std::function& space_checker) { + struct stat st; + if (stat(dirname.c_str(), &st) != 0) { + error(0, errno, "Unable to free space on %s", dirname.c_str()); + return false; } - std::set files = FindExpendableFiles(); - if (files.empty()) { - // nothing we can delete to free up space! - printf("no files can be deleted to free space on /cache\n"); - return -1; + if (!S_ISDIR(st.st_mode)) { + printf("%s is not a directory\n", dirname.c_str()); + return false; + } + + size_t free_now = space_checker(dirname); + printf("%zu bytes free on %s (%zu needed)\n", free_now, dirname.c_str(), bytes_needed); + + if (free_now >= bytes_needed) { + return true; } - // We could try to be smarter about which files to delete: the - // biggest ones? the smallest ones that will free up enough space? - // the oldest? the newest? - // - // Instead, we'll be dumb. + std::vector files; + if (dirname == CacheLocation::location().cache_log_directory()) { + // Deletes the log files only. + auto log_filter = [](const std::string& file_name) { + return android::base::StartsWith(file_name, "last_log") || + android::base::StartsWith(file_name, "last_kmsg"); + }; + + files = FindExpendableFiles(dirname, log_filter); + + // Older logs will come to the top of the queue. + auto comparator = [](const std::string& name1, const std::string& name2) -> bool { + unsigned int index1 = GetLogIndex(android::base::Basename(name1)); + unsigned int index2 = GetLogIndex(android::base::Basename(name2)); + if (index1 == index2) { + return name1 < name2; + } + + return index1 > index2; + }; + + std::sort(files.begin(), files.end(), comparator); + } else { + // We're allowed to delete unopened regular files in the directory. + files = FindExpendableFiles(dirname, nullptr); + } for (const auto& file : files) { - unlink(file.c_str()); - free_now = FreeSpaceForFile("/cache"); + if (unlink(file.c_str()) == -1) { + error(0, errno, "Failed to delete %s", file.c_str()); + continue; + } + + free_now = space_checker(dirname); printf("deleted %s; now %zu bytes free\n", file.c_str(), free_now); - if (free_now < bytes_needed) { - break; + if (free_now >= bytes_needed) { + return true; } } - return (free_now >= bytes_needed) ? 0 : -1; + + return false; } diff --git a/applypatch/include/applypatch/applypatch.h b/applypatch/include/applypatch/applypatch.h index 912ead1fa0..021a28d056 100644 --- a/applypatch/include/applypatch/applypatch.h +++ b/applypatch/include/applypatch/applypatch.h @@ -39,7 +39,7 @@ using SinkFn = std::function; // applypatch.cpp int ShowLicenses(); -size_t FreeSpaceForFile(const char* filename); +size_t FreeSpaceForFile(const std::string& filename); int CacheSizeCheck(size_t bytes); int ParseSha1(const char* str, uint8_t* digest); @@ -79,5 +79,9 @@ int ApplyImagePatch(const unsigned char* old_data, size_t old_size, const Value& // freecache.cpp int MakeFreeSpaceOnCache(size_t bytes_needed); +// Removes the files in |dirname| until we have at least |bytes_needed| bytes of free space on +// the partition. The size of the free space is returned by calling |space_checker|. +bool RemoveFilesInDirectory(size_t bytes_needed, const std::string& dirname, + const std::function& space_checker); #endif diff --git a/otautil/cache_location.cpp b/otautil/cache_location.cpp index 8ddefec5e9..6139bf17b9 100644 --- a/otautil/cache_location.cpp +++ b/otautil/cache_location.cpp @@ -19,6 +19,7 @@ constexpr const char kDefaultCacheTempSource[] = "/cache/saved.file"; constexpr const char kDefaultLastCommandFile[] = "/cache/recovery/last_command"; constexpr const char kDefaultStashDirectoryBase[] = "/cache/recovery"; +constexpr const char kDefaultCacheLogDirectory[] = "/cache/recovery"; CacheLocation& CacheLocation::location() { static CacheLocation cache_location; @@ -28,4 +29,5 @@ CacheLocation& CacheLocation::location() { CacheLocation::CacheLocation() : cache_temp_source_(kDefaultCacheTempSource), last_command_file_(kDefaultLastCommandFile), - stash_directory_base_(kDefaultStashDirectoryBase) {} + stash_directory_base_(kDefaultStashDirectoryBase), + cache_log_directory_(kDefaultCacheLogDirectory) {} diff --git a/otautil/include/otautil/cache_location.h b/otautil/include/otautil/cache_location.h index f2f663816e..005395e5f8 100644 --- a/otautil/include/otautil/cache_location.h +++ b/otautil/include/otautil/cache_location.h @@ -49,6 +49,13 @@ class CacheLocation { stash_directory_base_ = base; } + std::string cache_log_directory() const { + return cache_log_directory_; + } + void set_cache_log_directory(const std::string& log_dir) { + cache_log_directory_ = log_dir; + } + private: CacheLocation(); DISALLOW_COPY_AND_ASSIGN(CacheLocation); @@ -64,6 +71,9 @@ class CacheLocation { // The base directory to write stashes during update. std::string stash_directory_base_; + + // The location of last_log & last_kmsg. + std::string cache_log_directory_; }; #endif // _OTAUTIL_OTAUTIL_CACHE_LOCATION_H_ diff --git a/tests/component/applypatch_test.cpp b/tests/component/applypatch_test.cpp index 916028594c..aa0959bf51 100644 --- a/tests/component/applypatch_test.cpp +++ b/tests/component/applypatch_test.cpp @@ -14,8 +14,10 @@ * limitations under the License. */ +#include #include #include +#include #include #include #include @@ -23,6 +25,7 @@ #include #include +#include #include #include #include @@ -30,6 +33,7 @@ #include #include #include +#include #include #include @@ -110,6 +114,52 @@ class ApplyPatchModesTest : public ::testing::Test { TemporaryFile cache_source; }; +class FreeCacheTest : public ::testing::Test { + protected: + static constexpr size_t PARTITION_SIZE = 4096 * 10; + + // Returns a sorted list of files in |dirname|. + static std::vector FindFilesInDir(const std::string& dirname) { + std::vector file_list; + + std::unique_ptr d(opendir(dirname.c_str()), closedir); + struct dirent* de; + while ((de = readdir(d.get())) != 0) { + std::string path = dirname + "/" + de->d_name; + + struct stat st; + if (stat(path.c_str(), &st) == 0 && S_ISREG(st.st_mode)) { + file_list.emplace_back(de->d_name); + } + } + + std::sort(file_list.begin(), file_list.end()); + return file_list; + } + + static void AddFilesToDir(const std::string& dir, const std::vector& files) { + std::string zeros(4096, 0); + for (const auto& file : files) { + std::string path = dir + "/" + file; + ASSERT_TRUE(android::base::WriteStringToFile(zeros, path)); + } + } + + void SetUp() override { + CacheLocation::location().set_cache_log_directory(mock_log_dir.path); + } + + // A mock method to calculate the free space. It assumes the partition has a total size of 40960 + // bytes and all files are 4096 bytes in size. + size_t MockFreeSpaceChecker(const std::string& dirname) { + std::vector files = FindFilesInDir(dirname); + return PARTITION_SIZE - 4096 * files.size(); + } + + TemporaryDir mock_cache; + TemporaryDir mock_log_dir; +}; + TEST_F(ApplyPatchTest, CheckModeSkip) { std::vector sha1s; ASSERT_EQ(0, applypatch_check(&old_file[0], sha1s)); @@ -414,3 +464,82 @@ TEST_F(ApplyPatchModesTest, CheckModeInvalidArgs) { TEST_F(ApplyPatchModesTest, ShowLicenses) { ASSERT_EQ(0, applypatch_modes(2, (const char* []){ "applypatch", "-l" })); } + +TEST_F(FreeCacheTest, FreeCacheSmoke) { + std::vector files = { "file1", "file2", "file3" }; + AddFilesToDir(mock_cache.path, files); + ASSERT_EQ(files, FindFilesInDir(mock_cache.path)); + ASSERT_EQ(4096 * 7, MockFreeSpaceChecker(mock_cache.path)); + + ASSERT_TRUE(RemoveFilesInDirectory(4096 * 9, mock_cache.path, [&](const std::string& dir) { + return this->MockFreeSpaceChecker(dir); + })); + + ASSERT_EQ(std::vector{ "file3" }, FindFilesInDir(mock_cache.path)); + ASSERT_EQ(4096 * 9, MockFreeSpaceChecker(mock_cache.path)); +} + +TEST_F(FreeCacheTest, FreeCacheOpenFile) { + std::vector files = { "file1", "file2" }; + AddFilesToDir(mock_cache.path, files); + ASSERT_EQ(files, FindFilesInDir(mock_cache.path)); + ASSERT_EQ(4096 * 8, MockFreeSpaceChecker(mock_cache.path)); + + std::string file1_path = mock_cache.path + "/file1"s; + android::base::unique_fd fd(open(file1_path.c_str(), O_RDONLY)); + + // file1 can't be deleted as it's opened by us. + ASSERT_FALSE(RemoveFilesInDirectory(4096 * 10, mock_cache.path, [&](const std::string& dir) { + return this->MockFreeSpaceChecker(dir); + })); + + ASSERT_EQ(std::vector{ "file1" }, FindFilesInDir(mock_cache.path)); +} + +TEST_F(FreeCacheTest, FreeCacheLogsSmoke) { + std::vector log_files = { "last_log", "last_log.1", "last_kmsg.2", "last_log.5", + "last_log.10" }; + AddFilesToDir(mock_log_dir.path, log_files); + ASSERT_EQ(4096 * 5, MockFreeSpaceChecker(mock_log_dir.path)); + + ASSERT_TRUE(RemoveFilesInDirectory(4096 * 8, mock_log_dir.path, [&](const std::string& dir) { + return this->MockFreeSpaceChecker(dir); + })); + + // Logs with a higher index will be deleted first + std::vector expected = { "last_log", "last_log.1" }; + ASSERT_EQ(expected, FindFilesInDir(mock_log_dir.path)); + ASSERT_EQ(4096 * 8, MockFreeSpaceChecker(mock_log_dir.path)); +} + +TEST_F(FreeCacheTest, FreeCacheLogsStringComparison) { + std::vector log_files = { "last_log.1", "last_kmsg.1", "last_log.not_number", + "last_kmsgrandom" }; + AddFilesToDir(mock_log_dir.path, log_files); + ASSERT_EQ(4096 * 6, MockFreeSpaceChecker(mock_log_dir.path)); + + ASSERT_TRUE(RemoveFilesInDirectory(4096 * 9, mock_log_dir.path, [&](const std::string& dir) { + return this->MockFreeSpaceChecker(dir); + })); + + // Logs with incorrect format will be deleted first; and the last_kmsg with the same index is + // deleted before last_log. + std::vector expected = { "last_log.1" }; + ASSERT_EQ(expected, FindFilesInDir(mock_log_dir.path)); + ASSERT_EQ(4096 * 9, MockFreeSpaceChecker(mock_log_dir.path)); +} + +TEST_F(FreeCacheTest, FreeCacheLogsOtherFiles) { + std::vector log_files = { "last_install", "command", "block.map", "last_log", + "last_kmsg.1" }; + AddFilesToDir(mock_log_dir.path, log_files); + ASSERT_EQ(4096 * 5, MockFreeSpaceChecker(mock_log_dir.path)); + + ASSERT_FALSE(RemoveFilesInDirectory(4096 * 8, mock_log_dir.path, [&](const std::string& dir) { + return this->MockFreeSpaceChecker(dir); + })); + + // Non log files in /cache/recovery won't be deleted. + std::vector expected = { "block.map", "command", "last_install" }; + ASSERT_EQ(expected, FindFilesInDir(mock_log_dir.path)); +} From 3d0cd1d0360ab3e536be87798415fef60222aac4 Mon Sep 17 00:00:00 2001 From: Tao Bao Date: Fri, 13 Apr 2018 13:41:58 -0700 Subject: [PATCH 024/560] Expose PngHandler via resources.h. As a private header for testing purpose. PngHandler additionally loads a given filename if the one with '/res/images' prefix is not available. It also provides color_type/bit_depth that are parsed from the PNG file. This allows reusing the same code for the ResourcesTest (renamed from ResourceTest). Test: Run recovery_manual_test on marlin. Change-Id: I3f939d79a1cb1b83a899847dbe2d51bde15d16d8 --- minui/include/private/resources.h | 84 +++++++++++++++++ minui/resources.cpp | 71 +++----------- tests/manual/recovery_test.cpp | 150 +++++++++++------------------- 3 files changed, 152 insertions(+), 153 deletions(-) create mode 100644 minui/include/private/resources.h diff --git a/minui/include/private/resources.h b/minui/include/private/resources.h new file mode 100644 index 0000000000..2a83a1028f --- /dev/null +++ b/minui/include/private/resources.h @@ -0,0 +1,84 @@ +/* + * Copyright (C) 2018 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. + */ + +#pragma once + +#include + +#include +#include + +#include + +// This class handles the PNG file parsing. It also holds the ownership of the PNG pointer and the +// opened file pointer. Both will be destroyed / closed when this object goes out of scope. +class PngHandler { + public: + // Constructs an instance by loading the PNG file from '/res/images/.png', or ''. + PngHandler(const std::string& name); + + ~PngHandler(); + + png_uint_32 width() const { + return width_; + } + + png_uint_32 height() const { + return height_; + } + + png_byte channels() const { + return channels_; + } + + int bit_depth() const { + return bit_depth_; + } + + int color_type() const { + return color_type_; + } + + png_structp png_ptr() const { + return png_ptr_; + } + + png_infop info_ptr() const { + return info_ptr_; + } + + int error_code() const { + return error_code_; + }; + + operator bool() const { + return error_code_ == 0; + } + + private: + png_structp png_ptr_{ nullptr }; + png_infop info_ptr_{ nullptr }; + png_uint_32 width_; + png_uint_32 height_; + png_byte channels_; + int bit_depth_; + int color_type_; + + // The |error_code_| is set to a negative value if an error occurs when opening the png file. + int error_code_{ 0 }; + // After initialization, we'll keep the file pointer open before destruction of PngHandler. + std::unique_ptr png_fp_{ nullptr, fclose }; +}; diff --git a/minui/resources.cpp b/minui/resources.cpp index 52ab60b1b5..9f67cf8445 100644 --- a/minui/resources.cpp +++ b/minui/resources.cpp @@ -14,6 +14,8 @@ * limitations under the License. */ +#include "private/resources.h" + #include #include #include @@ -48,58 +50,13 @@ static GRSurface* malloc_surface(size_t data_size) { return surface; } -// This class handles the png file parsing. It also holds the ownership of the png pointer and the -// opened file pointer. Both will be destroyed/closed when this object goes out of scope. -class PngHandler { - public: - PngHandler(const std::string& name); - - ~PngHandler(); - - png_uint_32 width() const { - return width_; - } - - png_uint_32 height() const { - return height_; - } - - png_byte channels() const { - return channels_; - } - - png_structp png_ptr() const { - return png_ptr_; - } - - png_infop info_ptr() const { - return info_ptr_; - } - - int error_code() const { - return error_code_; - }; - - operator bool() const { - return error_code_ == 0; - } - - private: - png_structp png_ptr_{ nullptr }; - png_infop info_ptr_{ nullptr }; - png_uint_32 width_; - png_uint_32 height_; - png_byte channels_; - - // The |error_code_| is set to a negative value if an error occurs when opening the png file. - int error_code_; - // After initialization, we'll keep the file pointer open before destruction of PngHandler. - std::unique_ptr png_fp_; -}; - -PngHandler::PngHandler(const std::string& name) : error_code_(0), png_fp_(nullptr, fclose) { +PngHandler::PngHandler(const std::string& name) { std::string res_path = android::base::StringPrintf("/res/images/%s.png", name.c_str()); png_fp_.reset(fopen(res_path.c_str(), "rbe")); + // Try to read from |name| if the resource path does not work. + if (!png_fp_) { + png_fp_.reset(fopen(name.c_str(), "rbe")); + } if (!png_fp_) { error_code_ = -1; return; @@ -138,19 +95,17 @@ PngHandler::PngHandler(const std::string& name) : error_code_(0), png_fp_(nullpt png_set_sig_bytes(png_ptr_, sizeof(header)); png_read_info(png_ptr_, info_ptr_); - int color_type; - int bit_depth; - png_get_IHDR(png_ptr_, info_ptr_, &width_, &height_, &bit_depth, &color_type, nullptr, nullptr, + png_get_IHDR(png_ptr_, info_ptr_, &width_, &height_, &bit_depth_, &color_type_, nullptr, nullptr, nullptr); channels_ = png_get_channels(png_ptr_, info_ptr_); - if (bit_depth == 8 && channels_ == 3 && color_type == PNG_COLOR_TYPE_RGB) { + if (bit_depth_ == 8 && channels_ == 3 && color_type_ == PNG_COLOR_TYPE_RGB) { // 8-bit RGB images: great, nothing to do. - } else if (bit_depth <= 8 && channels_ == 1 && color_type == PNG_COLOR_TYPE_GRAY) { + } else if (bit_depth_ <= 8 && channels_ == 1 && color_type_ == PNG_COLOR_TYPE_GRAY) { // 1-, 2-, 4-, or 8-bit gray images: expand to 8-bit gray. png_set_expand_gray_1_2_4_to_8(png_ptr_); - } else if (bit_depth <= 8 && channels_ == 1 && color_type == PNG_COLOR_TYPE_PALETTE) { + } else if (bit_depth_ <= 8 && channels_ == 1 && color_type_ == PNG_COLOR_TYPE_PALETTE) { // paletted images: expand to 8-bit RGB. Note that we DON'T // currently expand the tRNS chunk (if any) to an alpha // channel, because minui doesn't support alpha channels in @@ -158,8 +113,8 @@ PngHandler::PngHandler(const std::string& name) : error_code_(0), png_fp_(nullpt png_set_palette_to_rgb(png_ptr_); channels_ = 3; } else { - fprintf(stderr, "minui doesn't support PNG depth %d channels %d color_type %d\n", bit_depth, - channels_, color_type); + fprintf(stderr, "minui doesn't support PNG depth %d channels %d color_type %d\n", bit_depth_, + channels_, color_type_); error_code_ = -7; } } diff --git a/tests/manual/recovery_test.cpp b/tests/manual/recovery_test.cpp index 224ed5b683..c992b07e20 100644 --- a/tests/manual/recovery_test.cpp +++ b/tests/manual/recovery_test.cpp @@ -15,10 +15,14 @@ */ #include +#include +#include +#include #include #include #include +#include #include #include @@ -26,13 +30,13 @@ #include #include #include -#include #include #include "minui/minui.h" +#include "private/resources.h" -static const std::string myFilename = "/data/misc/recovery/inject.txt"; -static const std::string myContent = "Hello World\nWelcome to my recovery\n"; +static const std::string kInjectTxtFilename = "/data/misc/recovery/inject.txt"; +static const std::string kInjectTxtContent = "Hello World\nWelcome to my recovery\n"; static const std::string kLocale = "zu"; // Failure is expected on systems that do not deliver either the @@ -43,9 +47,9 @@ static ssize_t __pmsg_fn(log_id_t logId, char prio, const char *filename, const char *buf, size_t len, void *arg) { EXPECT_EQ(LOG_ID_SYSTEM, logId); EXPECT_EQ(ANDROID_LOG_INFO, prio); - EXPECT_NE(std::string::npos, myFilename.find(filename)); - EXPECT_EQ(myContent, buf); - EXPECT_EQ(myContent.size(), len); + EXPECT_NE(std::string::npos, kInjectTxtFilename.find(filename)); + EXPECT_EQ(kInjectTxtContent, buf); + EXPECT_EQ(kInjectTxtContent.size(), len); EXPECT_EQ(nullptr, arg); return len; } @@ -58,13 +62,14 @@ TEST(recovery, refresh) { ssize_t ret = __android_log_pmsg_file_read( LOG_ID_SYSTEM, ANDROID_LOG_INFO, "recovery/", __pmsg_fn, nullptr); if (ret == -ENOENT) { - EXPECT_LT(0, __android_log_pmsg_file_write(LOG_ID_SYSTEM, ANDROID_LOG_INFO, - myFilename.c_str(), myContent.c_str(), myContent.size())); + EXPECT_LT(0, __android_log_pmsg_file_write( + LOG_ID_SYSTEM, ANDROID_LOG_INFO, kInjectTxtFilename.c_str(), + kInjectTxtContent.c_str(), kInjectTxtContent.size())); - fprintf(stderr, "injected test data, requires two intervening reboots " - "to check for replication\n"); + fprintf(stderr, + "injected test data, requires two intervening reboots to check for replication\n"); } - EXPECT_EQ(static_cast(myContent.size()), ret); + EXPECT_EQ(static_cast(kInjectTxtContent.size()), ret); } // recovery.persist - Requires recovery.inject, then a reboot, then @@ -75,30 +80,26 @@ TEST(recovery, persist) { ssize_t ret = __android_log_pmsg_file_read( LOG_ID_SYSTEM, ANDROID_LOG_INFO, "recovery/", __pmsg_fn, nullptr); if (ret == -ENOENT) { - EXPECT_LT(0, __android_log_pmsg_file_write(LOG_ID_SYSTEM, ANDROID_LOG_INFO, - myFilename.c_str(), myContent.c_str(), myContent.size())); + EXPECT_LT(0, __android_log_pmsg_file_write( + LOG_ID_SYSTEM, ANDROID_LOG_INFO, kInjectTxtFilename.c_str(), + kInjectTxtContent.c_str(), kInjectTxtContent.size())); - fprintf(stderr, "injected test data, requires intervening reboot " - "to check for storage\n"); + fprintf(stderr, "injected test data, requires intervening reboot to check for storage\n"); } std::string buf; - EXPECT_TRUE(android::base::ReadFileToString(myFilename, &buf)); - EXPECT_EQ(myContent, buf); - if (access(myFilename.c_str(), F_OK) == 0) { - fprintf(stderr, "Removing persistent test data, " - "check if reconstructed on reboot\n"); + EXPECT_TRUE(android::base::ReadFileToString(kInjectTxtFilename, &buf)); + EXPECT_EQ(kInjectTxtContent, buf); + if (access(kInjectTxtFilename.c_str(), F_OK) == 0) { + fprintf(stderr, "Removing persistent test data, check if reconstructed on reboot\n"); } - EXPECT_EQ(0, unlink(myFilename.c_str())); + EXPECT_EQ(0, unlink(kInjectTxtFilename.c_str())); } -std::vector image_dir { - "res-mdpi/images/", - "res-hdpi/images/", - "res-xhdpi/images/", - "res-xxhdpi/images/", - "res-xxxhdpi/images/" -}; +static const std::vector kResourceImagesDirs{ "res-mdpi/images/", "res-hdpi/images/", + "res-xhdpi/images/", + "res-xxhdpi/images/", + "res-xxxhdpi/images/" }; static int png_filter(const dirent* de) { if (de->d_type != DT_REG || !android::base::EndsWith(de->d_name, "_text.png")) { @@ -107,12 +108,12 @@ static int png_filter(const dirent* de) { return 1; } -// Find out all the PNG files to test, which stay under the same dir with the executable. +// Finds out all the PNG files to test, which stay under the same dir with the executable. static std::vector add_files() { std::string exec_dir = android::base::GetExecutableDirectory(); std::vector files; - for (const std::string& image : image_dir) { - std::string dir_path = exec_dir + "/" + image; + for (const std::string& images_dir : kResourceImagesDirs) { + std::string dir_path = exec_dir + "/" + images_dir; dirent** namelist; int n = scandir(dir_path.c_str(), &namelist, png_filter, alphasort); if (n == -1) { @@ -133,71 +134,29 @@ static std::vector add_files() { return files; } -class ResourceTest : public testing::TestWithParam { +class ResourcesTest : public testing::TestWithParam { public: static std::vector png_list; - // Parse a png file and test if it's qualified for the background text image - // under recovery. + protected: void SetUp() override { - std::string file_path = GetParam(); - fp = fopen(file_path.c_str(), "rbe"); - ASSERT_NE(nullptr, fp); - - unsigned char header[8]; - size_t bytesRead = fread(header, 1, sizeof(header), fp); - ASSERT_EQ(sizeof(header), bytesRead); - ASSERT_EQ(0, png_sig_cmp(header, 0, sizeof(header))); - - png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr); - ASSERT_NE(nullptr, png_ptr); - - info_ptr = png_create_info_struct(png_ptr); - ASSERT_NE(nullptr, info_ptr); - - png_init_io(png_ptr, fp); - png_set_sig_bytes(png_ptr, sizeof(header)); - png_read_info(png_ptr, info_ptr); - - int color_type, bit_depth; - png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth, &color_type, nullptr, nullptr, - nullptr); - ASSERT_EQ(PNG_COLOR_TYPE_GRAY, color_type) << "Recovery expects grayscale PNG file."; - ASSERT_LT(static_cast(5), width); - ASSERT_LT(static_cast(0), height); - if (bit_depth <= 8) { - // 1-, 2-, 4-, or 8-bit gray images: expand to 8-bit gray. - png_set_expand_gray_1_2_4_to_8(png_ptr); - } + png_ = std::make_unique(GetParam()); + ASSERT_TRUE(png_); - png_byte channels = png_get_channels(png_ptr, info_ptr); - ASSERT_EQ(1, channels) << "Recovery background text images expects 1-channel PNG file."; + ASSERT_EQ(PNG_COLOR_TYPE_GRAY, png_->color_type()) << "Recovery expects grayscale PNG file."; + ASSERT_LT(static_cast(5), png_->width()); + ASSERT_LT(static_cast(0), png_->height()); + ASSERT_EQ(1, png_->channels()) << "Recovery background text images expects 1-channel PNG file."; } - void TearDown() override { - if (png_ptr != nullptr && info_ptr != nullptr) { - png_destroy_read_struct(&png_ptr, &info_ptr, nullptr); - } - - if (fp != nullptr) { - fclose(fp); - } - } - - protected: - png_structp png_ptr; - png_infop info_ptr; - png_uint_32 width, height; - - FILE* fp; + std::unique_ptr png_{ nullptr }; }; -std::vector ResourceTest::png_list = add_files(); - -TEST_P(ResourceTest, ValidateLocale) { - std::vector row(width); - for (png_uint_32 y = 0; y < height; ++y) { - png_read_row(png_ptr, row.data(), nullptr); +// Parses a png file and tests if it's qualified for the background text image under recovery. +TEST_P(ResourcesTest, ValidateLocale) { + std::vector row(png_->width()); + for (png_uint_32 y = 0; y < png_->height(); ++y) { + png_read_row(png_->png_ptr(), row.data(), nullptr); int w = (row[1] << 8) | row[0]; int h = (row[3] << 8) | row[2]; int len = row[4]; @@ -206,19 +165,20 @@ TEST_P(ResourceTest, ValidateLocale) { EXPECT_LT(0, len) << "Locale string should be non-empty."; EXPECT_NE(0, row[5]) << "Locale string is missing."; - ASSERT_GT(height, y + 1 + h) << "Locale: " << kLocale << " is not found in the file."; + ASSERT_GT(png_->height(), y + 1 + h) << "Locale: " << kLocale << " is not found in the file."; char* loc = reinterpret_cast(&row[5]); if (matches_locale(loc, kLocale.c_str())) { EXPECT_TRUE(android::base::StartsWith(loc, kLocale)); break; - } else { - for (int i = 0; i < h; ++i, ++y) { - png_read_row(png_ptr, row.data(), nullptr); - } + } + for (int i = 0; i < h; ++i, ++y) { + png_read_row(png_->png_ptr(), row.data(), nullptr); } } } -INSTANTIATE_TEST_CASE_P(BackgroundTextValidation, ResourceTest, - ::testing::ValuesIn(ResourceTest::png_list.cbegin(), - ResourceTest::png_list.cend())); +std::vector ResourcesTest::png_list = add_files(); + +INSTANTIATE_TEST_CASE_P(BackgroundTextValidation, ResourcesTest, + ::testing::ValuesIn(ResourcesTest::png_list.cbegin(), + ResourcesTest::png_list.cend())); From 570b08b7904901162ae2d4c847b19c1aa9738aaf Mon Sep 17 00:00:00 2001 From: Josh Gao Date: Fri, 13 Apr 2018 16:08:01 -0700 Subject: [PATCH 025/560] minadbd: track signature change of service_to_fd. Bug: http://b/37066218 Bug: http://b/71898863 Test: treehugger Change-Id: I5f2b14c65cff8d41dd3230d78b87e3e27e489bf6 --- minadbd/minadbd_services.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/minadbd/minadbd_services.cpp b/minadbd/minadbd_services.cpp index 9f0f1f87dc..043c51a6a8 100644 --- a/minadbd/minadbd_services.cpp +++ b/minadbd/minadbd_services.cpp @@ -58,7 +58,7 @@ static int create_service_thread(void (*func)(int, const std::string&), const st return s[0]; } -int service_to_fd(const char* name, const atransport* /* transport */) { +int service_to_fd(const char* name, atransport* /* transport */) { int ret = -1; if (!strncmp(name, "sideload:", 9)) { From 83847e6ed9f774a3aa3875e115641d6642ae79d7 Mon Sep 17 00:00:00 2001 From: Chih-Hung Hsieh Date: Wed, 7 Mar 2018 21:59:52 -0800 Subject: [PATCH 026/560] Disable lld where it fails. Bug: 77543887 Test: make checkbuild and boot Change-Id: I4134a8eec592dac347c84cc7dc3f9f99f27bc50c --- Android.mk | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Android.mk b/Android.mk index 08eb739ddf..aea0957187 100644 --- a/Android.mk +++ b/Android.mk @@ -133,6 +133,10 @@ LOCAL_MODULE := recovery LOCAL_FORCE_STATIC_EXECUTABLE := true +# Cannot link with LLD: undefined symbol: UsbNoPermissionsLongHelpText +# http://b/77543887, lld does not handle -Wl,--gc-sections as well as ld. +LOCAL_USE_CLANG_LLD := false + LOCAL_REQUIRED_MODULES := e2fsdroid_static mke2fs_static mke2fs.conf ifeq ($(TARGET_USERIMAGES_USE_F2FS),true) From 6b28f05c5b9842bc0e1f8bdc103e4c2a79c01ed9 Mon Sep 17 00:00:00 2001 From: Tao Bao Date: Fri, 13 Apr 2018 13:41:58 -0700 Subject: [PATCH 027/560] tests: Move ResourcesTest into component test. Although the tests were initially written for checking the validity of the text images, it doesn't hurt to run them continuously as part of the component test (recovery_manual_test requires reboots during the run, due to the nature of the tests of recovery-{refresh,persist}). This also allows detecting breaking changes to libminui or libpng. There's a catch that the ResourcesTest won't be triggered via `atest`, as the res-* testdata won't be picked up via AndroidTest.xml. Explored a few options but not addressing that in this CL: - `atest` is not fully working in AOSP yet (missing support in tools/tradefederation/core/atest/atest.py). - `atest` doesn't allow specifying the testdata with path in the 'push' option. - It won't fail the test run though, as ResourcesTest will skip the tests automatically when it finds no text image file. - APCT and manual `adb sync data` are not affected, and I don't see an active user of `atest` other than a tool for manual test invocation. - Unrelated to this CL, `atest` doesn't seem to work well with recovery_component_test or recovery_unit_test while we have both of them in one AndroidTest.xml. It randomly triggers only one of them, despite of the given test name. When splitting AndroidTest.xml into two, it tends to pick up the wrong testdata subdir and gives wrong results. Test: Run recovery_manual_test and recovery_component_test on marlin. Change-Id: I3a237499a7770356e14085674bc8b9cb4551db85 --- tests/Android.mk | 12 +-- tests/component/resources_test.cpp | 120 +++++++++++++++++++++++++++++ tests/manual/recovery_test.cpp | 95 ----------------------- 3 files changed, 126 insertions(+), 101 deletions(-) create mode 100644 tests/component/resources_test.cpp diff --git a/tests/Android.mk b/tests/Android.mk index 58cefd2267..b29ff50f6e 100644 --- a/tests/Android.mk +++ b/tests/Android.mk @@ -54,17 +54,13 @@ include $(CLEAR_VARS) LOCAL_CFLAGS := -Wall -Werror LOCAL_MODULE := recovery_manual_test LOCAL_STATIC_LIBRARIES := \ - libminui \ libbase \ libBionicGtestMain LOCAL_SRC_FILES := manual/recovery_test.cpp LOCAL_SHARED_LIBRARIES := \ - liblog \ - libpng + liblog -LOCAL_TEST_DATA := \ - $(call find-test-data-in-subdirs, bootable/recovery, "*_text.png", res-*) include $(BUILD_NATIVE_TEST) # Component tests @@ -95,6 +91,7 @@ LOCAL_SRC_FILES := \ component/edify_test.cpp \ component/imgdiff_test.cpp \ component/install_test.cpp \ + component/resources_test.cpp \ component/sideload_test.cpp \ component/uncrypt_test.cpp \ component/updater_test.cpp \ @@ -121,6 +118,7 @@ LOCAL_STATIC_LIBRARIES := \ libbsdiff \ libbspatch \ libfusesideload \ + libminui \ libotafault \ librecovery \ libupdater \ @@ -132,6 +130,7 @@ LOCAL_STATIC_LIBRARIES := \ libdivsufsort \ libdivsufsort64 \ libfs_mgr \ + libpng \ libvintf_recovery \ libvintf \ libhidl-gen-utils \ @@ -157,7 +156,8 @@ LOCAL_STATIC_LIBRARIES := \ $(tune2fs_static_libraries) LOCAL_TEST_DATA := \ - $(call find-test-data-in-subdirs, $(LOCAL_PATH), "*", testdata) + $(call find-test-data-in-subdirs, $(LOCAL_PATH), "*", testdata) \ + $(call find-test-data-in-subdirs, bootable/recovery, "*_text.png", res-*) include $(BUILD_NATIVE_TEST) # Host tests diff --git a/tests/component/resources_test.cpp b/tests/component/resources_test.cpp new file mode 100644 index 0000000000..618d5a4dd3 --- /dev/null +++ b/tests/component/resources_test.cpp @@ -0,0 +1,120 @@ +/* + * Copyright (C) 2018 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 +#include +#include + +#include +#include +#include + +#include +#include +#include +#include + +#include "minui/minui.h" +#include "private/resources.h" + +static const std::string kLocale = "zu"; + +static const std::vector kResourceImagesDirs{ "res-mdpi/images/", "res-hdpi/images/", + "res-xhdpi/images/", + "res-xxhdpi/images/", + "res-xxxhdpi/images/" }; + +static int png_filter(const dirent* de) { + if (de->d_type != DT_REG || !android::base::EndsWith(de->d_name, "_text.png")) { + return 0; + } + return 1; +} + +// Finds out all the PNG files to test, which stay under the same dir with the executabl.. +static std::vector add_files() { + std::vector files; + for (const std::string& images_dir : kResourceImagesDirs) { + static std::string exec_dir = android::base::GetExecutableDirectory(); + std::string dir_path = exec_dir + "/" + images_dir; + dirent** namelist; + int n = scandir(dir_path.c_str(), &namelist, png_filter, alphasort); + if (n == -1) { + printf("Failed to scandir %s: %s\n", dir_path.c_str(), strerror(errno)); + continue; + } + if (n == 0) { + printf("No file is added for test in %s\n", dir_path.c_str()); + } + + while (n--) { + std::string file_path = dir_path + namelist[n]->d_name; + files.push_back(file_path); + free(namelist[n]); + } + free(namelist); + } + return files; +} + +class ResourcesTest : public testing::TestWithParam { + public: + static std::vector png_list; + + protected: + void SetUp() override { + png_ = std::make_unique(GetParam()); + ASSERT_TRUE(png_); + + ASSERT_EQ(PNG_COLOR_TYPE_GRAY, png_->color_type()) << "Recovery expects grayscale PNG file."; + ASSERT_LT(static_cast(5), png_->width()); + ASSERT_LT(static_cast(0), png_->height()); + ASSERT_EQ(1, png_->channels()) << "Recovery background text images expects 1-channel PNG file."; + } + + std::unique_ptr png_{ nullptr }; +}; + +// Parses a png file and tests if it's qualified for the background text image under recovery. +TEST_P(ResourcesTest, ValidateLocale) { + std::vector row(png_->width()); + for (png_uint_32 y = 0; y < png_->height(); ++y) { + png_read_row(png_->png_ptr(), row.data(), nullptr); + int w = (row[1] << 8) | row[0]; + int h = (row[3] << 8) | row[2]; + int len = row[4]; + EXPECT_LT(0, w); + EXPECT_LT(0, h); + EXPECT_LT(0, len) << "Locale string should be non-empty."; + EXPECT_NE(0, row[5]) << "Locale string is missing."; + + ASSERT_GT(png_->height(), y + 1 + h) << "Locale: " << kLocale << " is not found in the file."; + char* loc = reinterpret_cast(&row[5]); + if (matches_locale(loc, kLocale.c_str())) { + EXPECT_TRUE(android::base::StartsWith(loc, kLocale)); + break; + } + for (int i = 0; i < h; ++i, ++y) { + png_read_row(png_->png_ptr(), row.data(), nullptr); + } + } +} + +std::vector ResourcesTest::png_list = add_files(); + +INSTANTIATE_TEST_CASE_P(BackgroundTextValidation, ResourcesTest, + ::testing::ValuesIn(ResourcesTest::png_list.cbegin(), + ResourcesTest::png_list.cend())); diff --git a/tests/manual/recovery_test.cpp b/tests/manual/recovery_test.cpp index c992b07e20..e1d0771e79 100644 --- a/tests/manual/recovery_test.cpp +++ b/tests/manual/recovery_test.cpp @@ -14,30 +14,22 @@ * limitations under the License. */ -#include #include #include -#include #include #include #include #include #include -#include #include -#include #include #include #include -#include "minui/minui.h" -#include "private/resources.h" - static const std::string kInjectTxtFilename = "/data/misc/recovery/inject.txt"; static const std::string kInjectTxtContent = "Hello World\nWelcome to my recovery\n"; -static const std::string kLocale = "zu"; // Failure is expected on systems that do not deliver either the // recovery-persist or recovery-refresh executables. Tests also require @@ -95,90 +87,3 @@ TEST(recovery, persist) { } EXPECT_EQ(0, unlink(kInjectTxtFilename.c_str())); } - -static const std::vector kResourceImagesDirs{ "res-mdpi/images/", "res-hdpi/images/", - "res-xhdpi/images/", - "res-xxhdpi/images/", - "res-xxxhdpi/images/" }; - -static int png_filter(const dirent* de) { - if (de->d_type != DT_REG || !android::base::EndsWith(de->d_name, "_text.png")) { - return 0; - } - return 1; -} - -// Finds out all the PNG files to test, which stay under the same dir with the executable. -static std::vector add_files() { - std::string exec_dir = android::base::GetExecutableDirectory(); - std::vector files; - for (const std::string& images_dir : kResourceImagesDirs) { - std::string dir_path = exec_dir + "/" + images_dir; - dirent** namelist; - int n = scandir(dir_path.c_str(), &namelist, png_filter, alphasort); - if (n == -1) { - printf("Failed to scan dir %s: %s\n", exec_dir.c_str(), strerror(errno)); - return files; - } - if (n == 0) { - printf("No file is added for test in %s\n", exec_dir.c_str()); - } - - while (n--) { - std::string file_path = dir_path + namelist[n]->d_name; - files.push_back(file_path); - free(namelist[n]); - } - free(namelist); - } - return files; -} - -class ResourcesTest : public testing::TestWithParam { - public: - static std::vector png_list; - - protected: - void SetUp() override { - png_ = std::make_unique(GetParam()); - ASSERT_TRUE(png_); - - ASSERT_EQ(PNG_COLOR_TYPE_GRAY, png_->color_type()) << "Recovery expects grayscale PNG file."; - ASSERT_LT(static_cast(5), png_->width()); - ASSERT_LT(static_cast(0), png_->height()); - ASSERT_EQ(1, png_->channels()) << "Recovery background text images expects 1-channel PNG file."; - } - - std::unique_ptr png_{ nullptr }; -}; - -// Parses a png file and tests if it's qualified for the background text image under recovery. -TEST_P(ResourcesTest, ValidateLocale) { - std::vector row(png_->width()); - for (png_uint_32 y = 0; y < png_->height(); ++y) { - png_read_row(png_->png_ptr(), row.data(), nullptr); - int w = (row[1] << 8) | row[0]; - int h = (row[3] << 8) | row[2]; - int len = row[4]; - EXPECT_LT(0, w); - EXPECT_LT(0, h); - EXPECT_LT(0, len) << "Locale string should be non-empty."; - EXPECT_NE(0, row[5]) << "Locale string is missing."; - - ASSERT_GT(png_->height(), y + 1 + h) << "Locale: " << kLocale << " is not found in the file."; - char* loc = reinterpret_cast(&row[5]); - if (matches_locale(loc, kLocale.c_str())) { - EXPECT_TRUE(android::base::StartsWith(loc, kLocale)); - break; - } - for (int i = 0; i < h; ++i, ++y) { - png_read_row(png_->png_ptr(), row.data(), nullptr); - } - } -} - -std::vector ResourcesTest::png_list = add_files(); - -INSTANTIATE_TEST_CASE_P(BackgroundTextValidation, ResourcesTest, - ::testing::ValuesIn(ResourcesTest::png_list.cbegin(), - ResourcesTest::png_list.cend())); From 0e007e8edc7a3c1bd1076451269dad21c4672158 Mon Sep 17 00:00:00 2001 From: Zhomart Mukhamejanov Date: Tue, 17 Apr 2018 16:09:31 -0700 Subject: [PATCH 028/560] Add sample_updater style check to PREUPLOAD.cfg Test: N/A Change-Id: I13f4295a85d50c0a63cac5680e7a3200b0e6c909 --- PREUPLOAD.cfg | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/PREUPLOAD.cfg b/PREUPLOAD.cfg index b5f5f03628..878651c8e8 100644 --- a/PREUPLOAD.cfg +++ b/PREUPLOAD.cfg @@ -4,3 +4,8 @@ clang_format = true [Builtin Hooks Options] # Handle native codes only. clang_format = --commit ${PREUPLOAD_COMMIT} --style file --extensions c,h,cc,cpp + +[Hook Scripts] +checkstyle_hook = ${REPO_ROOT}/prebuilts/checkstyle/checkstyle.py --sha ${PREUPLOAD_COMMIT} + -fw sample_updater/ + From 4f834304715229dc6a94060e6b2a146a329b1f1c Mon Sep 17 00:00:00 2001 From: Tao Bao Date: Thu, 19 Apr 2018 12:35:14 -0700 Subject: [PATCH 029/560] applypatch: Dump patch info on mismatching patching result. After splitting the previously flaky ApplyPatchModesTest#PatchModeEmmcTarget tests, PatchModeEmmcTargetWithMultiplePatches now becomes the sole victim. This CL dumps additional info to narrow down the cause. Bug: 67849209 Test: `recovery_component_test` on marlin. Test: It dumps additional info after using corrupt bonus.file. Change-Id: Ic5436de457cc882a51d03f49d5cee70077f7d3df --- applypatch/applypatch.cpp | 17 ++++++++++++++++- tests/component/applypatch_test.cpp | 5 +++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/applypatch/applypatch.cpp b/applypatch/applypatch.cpp index 6f6c187bef..120add6d6d 100644 --- a/applypatch/applypatch.cpp +++ b/applypatch/applypatch.cpp @@ -652,7 +652,22 @@ static int GenerateTarget(const FileContents& source_file, const std::unique_ptr uint8_t current_target_sha1[SHA_DIGEST_LENGTH]; SHA1_Final(current_target_sha1, &ctx); if (memcmp(current_target_sha1, target_sha1, SHA_DIGEST_LENGTH) != 0) { - printf("patch did not produce expected sha1\n"); + printf("patch did not produce expected sha1 of %s\n", short_sha1(target_sha1).c_str()); + + printf("target size %zu sha1 %s\n", memory_sink_str.size(), + short_sha1(current_target_sha1).c_str()); + printf("source size %zu sha1 %s\n", source_file.data.size(), + short_sha1(source_file.sha1).c_str()); + + uint8_t patch_digest[SHA_DIGEST_LENGTH]; + SHA1(reinterpret_cast(patch->data.data()), patch->data.size(), patch_digest); + printf("patch size %zu sha1 %s\n", patch->data.size(), short_sha1(patch_digest).c_str()); + + uint8_t bonus_digest[SHA_DIGEST_LENGTH]; + SHA1(reinterpret_cast(bonus_data->data.data()), bonus_data->data.size(), + bonus_digest); + printf("bonus size %zu sha1 %s\n", bonus_data->data.size(), short_sha1(bonus_digest).c_str()); + return 1; } else { printf("now %s\n", short_sha1(target_sha1).c_str()); diff --git a/tests/component/applypatch_test.cpp b/tests/component/applypatch_test.cpp index aa0959bf51..f19f28c603 100644 --- a/tests/component/applypatch_test.cpp +++ b/tests/component/applypatch_test.cpp @@ -344,6 +344,11 @@ TEST_F(ApplyPatchModesTest, PatchModeEmmcTargetWithMultiplePatches) { patch1.c_str(), patch2.c_str(), patch3.c_str() }; + // TODO(b/67849209): Remove after addressing the flakiness. + printf("Calling applypatch_modes with the following args:\n"); + for (const auto& arg : args) { + printf(" %s\n", arg); + } ASSERT_EQ(0, applypatch_modes(args.size(), args.data())); } From 8b0b0f1f02f7990177e0da89eb471da2ced13f4c Mon Sep 17 00:00:00 2001 From: Tao Bao Date: Thu, 19 Apr 2018 21:02:13 -0700 Subject: [PATCH 030/560] applypatch: Drop the SHA_CTX parameter in Apply{BSDiff,Image}Patch. As they're accepting the SinkFn callback, it makes more sense to leave the work to their callers. Test: mmma -j bootable/recovery Test: Run recovery_component_test on marlin. Test: No other active user of the two functions. Change-Id: I8d67b38ce037925442296f136b483e0c71983777 --- applypatch/applypatch.cpp | 15 +++++++-------- applypatch/bspatch.cpp | 10 ++-------- applypatch/imgpatch.cpp | 19 +++++++------------ applypatch/include/applypatch/applypatch.h | 12 ++++++------ updater/blockimg.cpp | 5 ++--- 5 files changed, 24 insertions(+), 37 deletions(-) diff --git a/applypatch/applypatch.cpp b/applypatch/applypatch.cpp index 120add6d6d..7104abd67b 100644 --- a/applypatch/applypatch.cpp +++ b/applypatch/applypatch.cpp @@ -627,21 +627,20 @@ static int GenerateTarget(const FileContents& source_file, const std::unique_ptr // We store the decoded output in memory. std::string memory_sink_str; // Don't need to reserve space. - SinkFn sink = [&memory_sink_str](const unsigned char* data, size_t len) { + SHA_CTX ctx; + SHA1_Init(&ctx); + SinkFn sink = [&memory_sink_str, &ctx](const unsigned char* data, size_t len) { + SHA1_Update(&ctx, data, len); memory_sink_str.append(reinterpret_cast(data), len); return len; }; - SHA_CTX ctx; - SHA1_Init(&ctx); - int result; if (use_bsdiff) { - result = - ApplyBSDiffPatch(source_file.data.data(), source_file.data.size(), *patch, 0, sink, &ctx); + result = ApplyBSDiffPatch(source_file.data.data(), source_file.data.size(), *patch, 0, sink); } else { - result = ApplyImagePatch(source_file.data.data(), source_file.data.size(), *patch, sink, &ctx, - bonus_data); + result = + ApplyImagePatch(source_file.data.data(), source_file.data.size(), *patch, sink, bonus_data); } if (result != 0) { diff --git a/applypatch/bspatch.cpp b/applypatch/bspatch.cpp index 912dbbdd8e..ba33c3a9c6 100644 --- a/applypatch/bspatch.cpp +++ b/applypatch/bspatch.cpp @@ -66,18 +66,12 @@ void ShowBSDiffLicense() { } int ApplyBSDiffPatch(const unsigned char* old_data, size_t old_size, const Value& patch, - size_t patch_offset, SinkFn sink, SHA_CTX* ctx) { - auto sha_sink = [&sink, &ctx](const uint8_t* data, size_t len) { - len = sink(data, len); - if (ctx) SHA1_Update(ctx, data, len); - return len; - }; - + size_t patch_offset, SinkFn sink) { CHECK_LE(patch_offset, patch.data.size()); int result = bsdiff::bspatch(old_data, old_size, reinterpret_cast(&patch.data[patch_offset]), - patch.data.size() - patch_offset, sha_sink); + patch.data.size() - patch_offset, sink); if (result != 0) { LOG(ERROR) << "bspatch failed, result: " << result; // print SHA1 of the patch in the case of a data error. diff --git a/applypatch/imgpatch.cpp b/applypatch/imgpatch.cpp index 3682d61157..2e4faaadfe 100644 --- a/applypatch/imgpatch.cpp +++ b/applypatch/imgpatch.cpp @@ -51,7 +51,7 @@ static inline int32_t Read4(const void *address) { // patched data and stream the deflated data to output. static bool ApplyBSDiffPatchAndStreamOutput(const uint8_t* src_data, size_t src_len, const Value& patch, size_t patch_offset, - const char* deflate_header, SinkFn sink, SHA_CTX* ctx) { + const char* deflate_header, SinkFn sink) { size_t expected_target_length = static_cast(Read8(deflate_header + 32)); int level = Read4(deflate_header + 40); int method = Read4(deflate_header + 44); @@ -77,7 +77,7 @@ static bool ApplyBSDiffPatchAndStreamOutput(const uint8_t* src_data, size_t src_ size_t total_written = 0; static constexpr size_t buffer_size = 32768; auto compression_sink = [&strm, &actual_target_length, &expected_target_length, &total_written, - &ret, &ctx, &sink](const uint8_t* data, size_t len) -> size_t { + &ret, &sink](const uint8_t* data, size_t len) -> size_t { // The input patch length for an update never exceeds INT_MAX. strm.avail_in = len; strm.next_in = data; @@ -102,15 +102,13 @@ static bool ApplyBSDiffPatchAndStreamOutput(const uint8_t* src_data, size_t src_ LOG(ERROR) << "Failed to write " << have << " compressed bytes to output."; return 0; } - if (ctx) SHA1_Update(ctx, buffer.data(), have); } while ((strm.avail_in != 0 || strm.avail_out == 0) && ret != Z_STREAM_END); actual_target_length += len; return len; }; - int bspatch_result = - ApplyBSDiffPatch(src_data, src_len, patch, patch_offset, compression_sink, nullptr); + int bspatch_result = ApplyBSDiffPatch(src_data, src_len, patch, patch_offset, compression_sink); deflateEnd(&strm); if (bspatch_result != 0) { @@ -135,11 +133,11 @@ static bool ApplyBSDiffPatchAndStreamOutput(const uint8_t* src_data, size_t src_ int ApplyImagePatch(const unsigned char* old_data, size_t old_size, const unsigned char* patch_data, size_t patch_size, SinkFn sink) { Value patch(VAL_BLOB, std::string(reinterpret_cast(patch_data), patch_size)); - return ApplyImagePatch(old_data, old_size, patch, sink, nullptr, nullptr); + return ApplyImagePatch(old_data, old_size, patch, sink, nullptr); } int ApplyImagePatch(const unsigned char* old_data, size_t old_size, const Value& patch, SinkFn sink, - SHA_CTX* ctx, const Value* bonus_data) { + const Value* bonus_data) { if (patch.data.size() < 12) { printf("patch too short to contain header\n"); return -1; @@ -180,7 +178,7 @@ int ApplyImagePatch(const unsigned char* old_data, size_t old_size, const Value& printf("source data too short\n"); return -1; } - if (ApplyBSDiffPatch(old_data + src_start, src_len, patch, patch_offset, sink, ctx) != 0) { + if (ApplyBSDiffPatch(old_data + src_start, src_len, patch, patch_offset, sink) != 0) { printf("Failed to apply bsdiff patch.\n"); return -1; } @@ -198,9 +196,6 @@ int ApplyImagePatch(const unsigned char* old_data, size_t old_size, const Value& printf("failed to read chunk %d raw data\n", i); return -1; } - if (ctx) { - SHA1_Update(ctx, patch_header + pos, data_len); - } if (sink(reinterpret_cast(patch_header + pos), data_len) != data_len) { printf("failed to write chunk %d raw data\n", i); return -1; @@ -276,7 +271,7 @@ int ApplyImagePatch(const unsigned char* old_data, size_t old_size, const Value& } if (!ApplyBSDiffPatchAndStreamOutput(expanded_source.data(), expanded_len, patch, - patch_offset, deflate_header, sink, ctx)) { + patch_offset, deflate_header, sink)) { LOG(ERROR) << "Fail to apply streaming bspatch."; return -1; } diff --git a/applypatch/include/applypatch/applypatch.h b/applypatch/include/applypatch/applypatch.h index 021a28d056..77125f9de4 100644 --- a/applypatch/include/applypatch/applypatch.h +++ b/applypatch/include/applypatch/applypatch.h @@ -63,18 +63,18 @@ int SaveFileContents(const char* filename, const FileContents* file); void ShowBSDiffLicense(); // Applies the bsdiff-patch given in 'patch' (from offset 'patch_offset' to the end) to the source -// data given by (old_data, old_size). Writes the patched output through the given 'sink', and -// updates the SHA-1 context with the output data. Returns 0 on success. +// data given by (old_data, old_size). Writes the patched output through the given 'sink'. Returns +// 0 on success. int ApplyBSDiffPatch(const unsigned char* old_data, size_t old_size, const Value& patch, - size_t patch_offset, SinkFn sink, SHA_CTX* ctx); + size_t patch_offset, SinkFn sink); // imgpatch.cpp // Applies the imgdiff-patch given in 'patch' to the source data given by (old_data, old_size), with -// the optional bonus data. Writes the patched output through the given 'sink', and updates the -// SHA-1 context with the output data. Returns 0 on success. +// the optional bonus data. Writes the patched output through the given 'sink'. Returns 0 on +// success. int ApplyImagePatch(const unsigned char* old_data, size_t old_size, const Value& patch, SinkFn sink, - SHA_CTX* ctx, const Value* bonus_data); + const Value* bonus_data); // freecache.cpp diff --git a/updater/blockimg.cpp b/updater/blockimg.cpp index e93196b278..e72ddd313b 100644 --- a/updater/blockimg.cpp +++ b/updater/blockimg.cpp @@ -1429,7 +1429,7 @@ static int PerformCommandDiff(CommandParameters& params) { if (ApplyImagePatch(params.buffer.data(), blocks * BLOCKSIZE, patch_value, std::bind(&RangeSinkWriter::Write, &writer, std::placeholders::_1, std::placeholders::_2), - nullptr, nullptr) != 0) { + nullptr) != 0) { LOG(ERROR) << "Failed to apply image patch."; failure_type = kPatchApplicationFailure; return -1; @@ -1437,8 +1437,7 @@ static int PerformCommandDiff(CommandParameters& params) { } else { if (ApplyBSDiffPatch(params.buffer.data(), blocks * BLOCKSIZE, patch_value, 0, std::bind(&RangeSinkWriter::Write, &writer, std::placeholders::_1, - std::placeholders::_2), - nullptr) != 0) { + std::placeholders::_2)) != 0) { LOG(ERROR) << "Failed to apply bsdiff patch."; failure_type = kPatchApplicationFailure; return -1; From 1cc03519155fc333b7db6c90c4d82f70c246997a Mon Sep 17 00:00:00 2001 From: Tao Bao Date: Wed, 18 Apr 2018 17:10:49 -0700 Subject: [PATCH 031/560] Make update_verifier generic across verified boot versions. This allows the update_verifier in a general system image to work across devices that have different verified boot versions (i.e. not supported / verified boot 1.0 / verified boot 2.0 / disabled). Bug: 78283982 Test: Run recovery_component_test on both of marlin and walleye. Test: Generate an OTA that has this CL. Install this OTA and check the update_verifier log during the post-reboot verification, on both of marlin (VB 1.0) and walleye (VB 2.0). Test: Build and flash walleye image with verified boot disabled. Check that update_verifier marks the slot as successfully booted. Change-Id: I828d87d59f911786531f774ffcf9b2ad7c2ca007 --- tests/Android.mk | 8 ----- tests/component/update_verifier_test.cpp | 9 +++-- update_verifier/Android.mk | 8 ----- update_verifier/update_verifier.cpp | 45 ++++++++++-------------- 4 files changed, 22 insertions(+), 48 deletions(-) diff --git a/tests/Android.mk b/tests/Android.mk index b29ff50f6e..fd4497858a 100644 --- a/tests/Android.mk +++ b/tests/Android.mk @@ -74,14 +74,6 @@ ifeq ($(AB_OTA_UPDATER),true) LOCAL_CFLAGS += -DAB_OTA_UPDATER=1 endif -ifeq ($(PRODUCTS.$(INTERNAL_PRODUCT).PRODUCT_SUPPORTS_VERITY),true) -LOCAL_CFLAGS += -DPRODUCT_SUPPORTS_VERITY=1 -endif - -ifeq ($(BOARD_AVB_ENABLE),true) -LOCAL_CFLAGS += -DBOARD_AVB_ENABLE=1 -endif - LOCAL_MODULE := recovery_component_test LOCAL_COMPATIBILITY_SUITE := device-tests LOCAL_C_INCLUDES := bootable/recovery diff --git a/tests/component/update_verifier_test.cpp b/tests/component/update_verifier_test.cpp index 1544bb2a46..f6ef6dcfd2 100644 --- a/tests/component/update_verifier_test.cpp +++ b/tests/component/update_verifier_test.cpp @@ -17,6 +17,8 @@ #include #include +#include +#include #include #include #include @@ -24,11 +26,8 @@ class UpdateVerifierTest : public ::testing::Test { protected: void SetUp() override { -#if defined(PRODUCT_SUPPORTS_VERITY) || defined(BOARD_AVB_ENABLE) - verity_supported = true; -#else - verity_supported = false; -#endif + std::string verity_mode = android::base::GetProperty("ro.boot.veritymode", ""); + verity_supported = android::base::EqualsIgnoreCase(verity_mode, "enforcing"); } bool verity_supported; diff --git a/update_verifier/Android.mk b/update_verifier/Android.mk index 0ff88546f6..8d61cd1016 100644 --- a/update_verifier/Android.mk +++ b/update_verifier/Android.mk @@ -39,14 +39,6 @@ LOCAL_EXPORT_C_INCLUDE_DIRS := \ LOCAL_C_INCLUDES := \ $(LOCAL_PATH)/include -ifeq ($(PRODUCTS.$(INTERNAL_PRODUCT).PRODUCT_SUPPORTS_VERITY),true) -LOCAL_CFLAGS += -DPRODUCT_SUPPORTS_VERITY=1 -endif - -ifeq ($(BOARD_AVB_ENABLE),true) -LOCAL_CFLAGS += -DBOARD_AVB_ENABLE=1 -endif - include $(BUILD_STATIC_LIBRARY) # update_verifier (executable) diff --git a/update_verifier/update_verifier.cpp b/update_verifier/update_verifier.cpp index 92d9313710..dc72763268 100644 --- a/update_verifier/update_verifier.cpp +++ b/update_verifier/update_verifier.cpp @@ -15,24 +15,26 @@ */ /* - * This program verifies the integrity of the partitions after an A/B OTA - * update. It gets invoked by init, and will only perform the verification if - * it's the first boot post an A/B OTA update. + * update_verifier verifies the integrity of the partitions after an A/B OTA update. It gets invoked + * by init, and will only perform the verification if it's the first boot post an A/B OTA update + * (https://source.android.com/devices/tech/ota/ab/#after_reboot). * - * Update_verifier relies on dm-verity to capture any corruption on the partitions - * being verified. And its behavior varies depending on the dm-verity mode. - * Upon detection of failures: + * update_verifier relies on device-mapper-verity (dm-verity) to capture any corruption on the + * partitions being verified (https://source.android.com/security/verifiedboot). The verification + * will be skipped, if dm-verity is not enabled on the device. + * + * Upon detecting verification failures, the device will be rebooted, although the trigger of the + * reboot depends on the dm-verity mode. * enforcing mode: dm-verity reboots the device * eio mode: dm-verity fails the read and update_verifier reboots the device * other mode: not supported and update_verifier reboots the device * - * After a predefined number of failing boot attempts, the bootloader should - * 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. + * All these reboots prevent the device from booting into a known corrupt state. If the device + * continuously fails to boot into the new slot, the bootloader should mark the slot as unbootable + * and trigger a fallback to the old slot. * - * The current slot will be marked as having booted successfully if the - * verifier reaches the end after the verification. + * The current slot will be marked as having booted successfully if the verifier reaches the end + * after the verification. */ #include "update_verifier/update_verifier.h" @@ -103,12 +105,10 @@ static bool read_blocks(const std::string& partition, const std::string& range_s PLOG(WARNING) << "Failed to read " << path; } else { std::string dm_block_name = android::base::Trim(content); -#ifdef BOARD_AVB_ENABLE // AVB is using 'vroot' for the root block device but we're expecting 'system'. if (dm_block_name == "vroot") { dm_block_name = "system"; } -#endif if (dm_block_name == partition) { dm_block_device = DEV_PATH + std::string(namelist[n]->d_name); while (n--) { @@ -264,19 +264,13 @@ int update_verifier(int argc, char** argv) { if (is_successful == BoolResult::FALSE) { // The current slot has not booted successfully. -#if defined(PRODUCT_SUPPORTS_VERITY) || defined(BOARD_AVB_ENABLE) bool skip_verification = false; std::string verity_mode = android::base::GetProperty("ro.boot.veritymode", ""); if (verity_mode.empty()) { - // With AVB it's possible to disable verification entirely and - // in this case ro.boot.veritymode is empty. -#if defined(BOARD_AVB_ENABLE) - LOG(WARNING) << "verification has been disabled; marking without verification."; + // Skip the verification if ro.boot.veritymode property is not set. This could be a result + // that device doesn't support dm-verity, or has disabled that. + LOG(WARNING) << "dm-verity not enabled; marking without verification."; skip_verification = true; -#else - LOG(ERROR) << "Failed to get dm-verity mode."; - return reboot_device(); -#endif } else if (android::base::EqualsIgnoreCase(verity_mode, "eio")) { // We shouldn't see verity in EIO mode if the current slot hasn't booted successfully before. // Continue the verification until we fail to read some blocks. @@ -285,7 +279,7 @@ int update_verifier(int argc, char** argv) { LOG(WARNING) << "dm-verity in disabled mode; marking without verification."; skip_verification = true; } else if (verity_mode != "enforcing") { - LOG(ERROR) << "Unexpected dm-verity mode : " << verity_mode << ", expecting enforcing."; + LOG(ERROR) << "Unexpected dm-verity mode: " << verity_mode << ", expecting enforcing."; return reboot_device(); } @@ -296,9 +290,6 @@ int update_verifier(int argc, char** argv) { return reboot_device(); } } -#else - LOG(WARNING) << "dm-verity not enabled; marking without verification."; -#endif CommandResult cr; module->markBootSuccessful([&cr](CommandResult result) { cr = result; }); From afb9fc29a2255f914973814af3b3f187a1bc1530 Mon Sep 17 00:00:00 2001 From: Tao Bao Date: Thu, 19 Apr 2018 14:49:32 -0700 Subject: [PATCH 032/560] update_verifier: Move to Soong. Test: mmma -j bootable/recovery Change-Id: I3a3574c89318304231c01f7633d32ece31df098c --- Android.mk | 1 - update_verifier/Android.bp | 83 ++++++++++++++++++++++++++++++++++++++ update_verifier/Android.mk | 69 ------------------------------- 3 files changed, 83 insertions(+), 70 deletions(-) create mode 100644 update_verifier/Android.bp delete mode 100644 update_verifier/Android.mk diff --git a/Android.mk b/Android.mk index aea0957187..e54db2d1be 100644 --- a/Android.mk +++ b/Android.mk @@ -203,4 +203,3 @@ include \ $(LOCAL_PATH)/tests/Android.mk \ $(LOCAL_PATH)/tools/Android.mk \ $(LOCAL_PATH)/updater/Android.mk \ - $(LOCAL_PATH)/update_verifier/Android.mk \ diff --git a/update_verifier/Android.bp b/update_verifier/Android.bp new file mode 100644 index 0000000000..f6c7056d9e --- /dev/null +++ b/update_verifier/Android.bp @@ -0,0 +1,83 @@ +// Copyright (C) 2018 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. + +cc_defaults { + name: "update_verifier_defaults", + + cflags: [ + "-Wall", + "-Werror", + ], + + local_include_dirs: [ + "include", + ], +} + +cc_library_static { + name: "libupdate_verifier", + + defaults: [ + "update_verifier_defaults", + ], + + srcs: [ + "update_verifier.cpp", + ], + + export_include_dirs: [ + "include", + ], + + static_libs: [ + "libotautil", + ], + + shared_libs: [ + "android.hardware.boot@1.0", + "libbase", + "libcutils", + ], +} + +cc_binary { + name: "update_verifier", + + defaults: [ + "update_verifier_defaults", + ], + + srcs: [ + "update_verifier_main.cpp", + ], + + static_libs: [ + "libupdate_verifier", + "libotautil", + ], + + shared_libs: [ + "android.hardware.boot@1.0", + "libbase", + "libcutils", + "libhardware", + "libhidlbase", + "liblog", + "libutils", + ], + + init_rc: [ + "update_verifier.rc", + ], +} diff --git a/update_verifier/Android.mk b/update_verifier/Android.mk deleted file mode 100644 index 8d61cd1016..0000000000 --- a/update_verifier/Android.mk +++ /dev/null @@ -1,69 +0,0 @@ -# Copyright (C) 2015 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) - -# libupdate_verifier (static library) -# =============================== -include $(CLEAR_VARS) - -LOCAL_SRC_FILES := \ - update_verifier.cpp - -LOCAL_MODULE := libupdate_verifier - -LOCAL_STATIC_LIBRARIES := \ - libotautil - -LOCAL_SHARED_LIBRARIES := \ - libbase \ - libcutils \ - android.hardware.boot@1.0 - -LOCAL_CFLAGS := -Wall -Werror - -LOCAL_EXPORT_C_INCLUDE_DIRS := \ - $(LOCAL_PATH)/include - -LOCAL_C_INCLUDES := \ - $(LOCAL_PATH)/include - -include $(BUILD_STATIC_LIBRARY) - -# update_verifier (executable) -# =============================== -include $(CLEAR_VARS) - -LOCAL_SRC_FILES := \ - update_verifier_main.cpp - -LOCAL_MODULE := update_verifier -LOCAL_STATIC_LIBRARIES := \ - libupdate_verifier \ - libotautil - -LOCAL_SHARED_LIBRARIES := \ - libbase \ - libcutils \ - libhardware \ - liblog \ - libutils \ - libhidlbase \ - android.hardware.boot@1.0 - -LOCAL_CFLAGS := -Wall -Werror - -LOCAL_INIT_RC := update_verifier.rc - -include $(BUILD_EXECUTABLE) From ffed57a7a3e5e82969b5998f759d09fbb6edd4e2 Mon Sep 17 00:00:00 2001 From: Tianjie Xu Date: Mon, 23 Apr 2018 17:51:45 -0700 Subject: [PATCH 033/560] Dump debug information for apply_patch unit tests The apply patch test should have a deterministic way to append patch data. Add debug logs to dump the length and SHA1 of each step to further track down the flakiness. Also redirect the debug logging to stdout in case the logcat becomes too chatty. Bug: 67849209 Test: Run recovery_component_test Change-Id: I42bafef2d9dee599719ae57840b3d8c00d243ebd --- applypatch/applypatch.cpp | 5 +++++ applypatch/imgpatch.cpp | 5 +++++ tests/component/applypatch_test.cpp | 13 ++++++++++++- 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/applypatch/applypatch.cpp b/applypatch/applypatch.cpp index 7104abd67b..db7530be98 100644 --- a/applypatch/applypatch.cpp +++ b/applypatch/applypatch.cpp @@ -630,6 +630,11 @@ static int GenerateTarget(const FileContents& source_file, const std::unique_ptr SHA_CTX ctx; SHA1_Init(&ctx); SinkFn sink = [&memory_sink_str, &ctx](const unsigned char* data, size_t len) { + if (len != 0) { + uint8_t digest[SHA_DIGEST_LENGTH]; + SHA1(data, len, digest); + LOG(DEBUG) << "Appending " << len << " bytes data, sha1: " << short_sha1(digest); + } SHA1_Update(&ctx, data, len); memory_sink_str.append(reinterpret_cast(data), len); return len; diff --git a/applypatch/imgpatch.cpp b/applypatch/imgpatch.cpp index 2e4faaadfe..9794a4878a 100644 --- a/applypatch/imgpatch.cpp +++ b/applypatch/imgpatch.cpp @@ -182,6 +182,8 @@ int ApplyImagePatch(const unsigned char* old_data, size_t old_size, const Value& printf("Failed to apply bsdiff patch.\n"); return -1; } + + LOG(DEBUG) << "Processed chunk type normal"; } else if (type == CHUNK_RAW) { const char* raw_header = patch_header + pos; pos += 4; @@ -201,6 +203,8 @@ int ApplyImagePatch(const unsigned char* old_data, size_t old_size, const Value& return -1; } pos += data_len; + + LOG(DEBUG) << "Processed chunk type raw"; } else if (type == CHUNK_DEFLATE) { // deflate chunks have an additional 60 bytes in their chunk header. const char* deflate_header = patch_header + pos; @@ -276,6 +280,7 @@ int ApplyImagePatch(const unsigned char* old_data, size_t old_size, const Value& return -1; } + LOG(DEBUG) << "Processed chunk type deflate"; } else { printf("patch chunk %d is unknown type %d\n", i, type); return -1; diff --git a/tests/component/applypatch_test.cpp b/tests/component/applypatch_test.cpp index f19f28c603..292d76e43b 100644 --- a/tests/component/applypatch_test.cpp +++ b/tests/component/applypatch_test.cpp @@ -31,6 +31,7 @@ #include #include +#include #include #include #include @@ -46,7 +47,7 @@ using namespace std::string_literals; static void sha1sum(const std::string& fname, std::string* sha1, size_t* fsize = nullptr) { - ASSERT_NE(nullptr, sha1); + ASSERT_TRUE(sha1 != nullptr); std::string data; ASSERT_TRUE(android::base::ReadFileToString(fname, &data)); @@ -68,6 +69,14 @@ static void mangle_file(const std::string& fname) { ASSERT_TRUE(android::base::WriteStringToFile(content, fname)); } +static void test_logger(android::base::LogId /* id */, android::base::LogSeverity severity, + const char* /* tag */, const char* /* file */, unsigned int /* line */, + const char* message) { + if (severity >= android::base::GetMinimumLogSeverity()) { + fprintf(stdout, "%s\n", message); + } +} + class ApplyPatchTest : public ::testing::Test { public: virtual void SetUp() override { @@ -109,6 +118,8 @@ class ApplyPatchModesTest : public ::testing::Test { protected: void SetUp() override { CacheLocation::location().set_cache_temp_source(cache_source.path); + android::base::InitLogging(nullptr, &test_logger); + android::base::SetMinimumLogSeverity(android::base::LogSeverity::DEBUG); } TemporaryFile cache_source; From 3b4d516151c622b4015fb14af4ea3223ed57f2e0 Mon Sep 17 00:00:00 2001 From: Paul Crowley Date: Wed, 8 Jun 2016 13:51:41 -0700 Subject: [PATCH 034/560] Wipe the metadata partition when we wipe data. Bug: 78469201 Test: Wipe from recovery menu, check that wipe is logged correctly and boot works as expected. Change-Id: I5bc8ef1b83d78de8b5edba6cc17882edcc744356 --- recovery.cpp | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/recovery.cpp b/recovery.cpp index 910cd8296f..ceb19413ad 100644 --- a/recovery.cpp +++ b/recovery.cpp @@ -108,6 +108,7 @@ static const char *CONVERT_FBE_DIR = "/tmp/convert_fbe"; static const char *CONVERT_FBE_FILE = "/tmp/convert_fbe/convert_fbe"; static const char *CACHE_ROOT = "/cache"; static const char *DATA_ROOT = "/data"; +static const char* METADATA_ROOT = "/metadata"; static const char *SDCARD_ROOT = "/sdcard"; static const char *TEMPORARY_LOG_FILE = "/tmp/recovery.log"; static const char *TEMPORARY_INSTALL_FILE = "/tmp/last_install"; @@ -752,11 +753,19 @@ static bool wipe_data(Device* device) { modified_flash = true; ui->Print("\n-- Wiping data...\n"); - bool success = - device->PreWipeData() && - erase_volume("/data") && - (has_cache ? erase_volume("/cache") : true) && - device->PostWipeData(); + bool success = device->PreWipeData(); + if (success) { + success &= erase_volume(DATA_ROOT); + if (has_cache) { + success &= erase_volume(CACHE_ROOT); + } + if (volume_for_mount_point(METADATA_ROOT) != nullptr) { + success &= erase_volume(METADATA_ROOT); + } + } + if (success) { + success &= device->PostWipeData(); + } ui->Print("Data wipe %s.\n", success ? "complete" : "failed"); return success; } From 188f3a5bf8ff0f28e12a5977b7b21200f088aca7 Mon Sep 17 00:00:00 2001 From: Tao Bao Date: Tue, 24 Apr 2018 14:15:14 -0700 Subject: [PATCH 035/560] updater: Remove an unneeded strdup(). StringValue() makes a copy of the arg. Test: mmma bootable/recovery Change-Id: I1002994e7496c840aa05c785019bdeca281f467e --- updater/blockimg.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/updater/blockimg.cpp b/updater/blockimg.cpp index e72ddd313b..e7d213a717 100644 --- a/updater/blockimg.cpp +++ b/updater/blockimg.cpp @@ -1697,7 +1697,7 @@ static Value* PerformBlockImageUpdate(const char* name, State* state, for (size_t i = 0; i < cmdcount; ++i) { if (cmd_map.find(commands[i].name) != cmd_map.end()) { LOG(ERROR) << "Error: command [" << commands[i].name << "] already exists in the cmd map."; - return StringValue(strdup("")); + return StringValue(""); } cmd_map[commands[i].name] = &commands[i]; } From f4d280ce40ba0aa50d6b2c70d8031ddf7678d179 Mon Sep 17 00:00:00 2001 From: Zhomart Mukhamejanov Date: Tue, 17 Apr 2018 13:20:22 -0700 Subject: [PATCH 036/560] sample_updater: add non-streaming demo SampleUpdater app was tested manually on a device. There are unit tests for utility classes. SampleUpdater app demonstrates how to use Android Update Engine to apply A/B (seamless) update. This CL contains demo of non-stream update using async update_engine, which is accessed directly from an activity. This app also shows logs from update_engine on the UI. Instructions can be found in `README.md`. - Create a UI with list of configs, current version, control buttons and a progress bar - Add PayloadSpec and PayloadSpecs for working with update zip file - Add UpdateConfig for working with json config files - Add applying non-streaming update Test: tested manually and unit tests for utilities Change-Id: I05d4a46ad9cf8b334c9c60c7dd4da486dac0400a Signed-off-by: Zhomart Mukhamejanov --- sample_updater/.gitignore | 9 + sample_updater/Android.mk | 11 +- sample_updater/AndroidManifest.xml | 27 +- sample_updater/README.md | 73 +++- sample_updater/res/layout/activity_main.xml | 171 +++++++++- .../res/mipmap-hdpi/ic_launcher.png | Bin 0 -> 3056 bytes .../res/mipmap-hdpi/ic_launcher_round.png | Bin 0 -> 5024 bytes sample_updater/res/raw/sample.json | 22 ++ sample_updater/res/values/strings.xml | 21 ++ .../update/ui/SystemUpdateActivity.java | 68 ---- .../systemupdatersample/PayloadSpec.java | 122 +++++++ .../systemupdatersample/UpdateConfig.java | 183 ++++++++++ .../systemupdatersample/ui/MainActivity.java | 314 ++++++++++++++++++ .../updates/AbNonStreamingUpdate.java | 52 +++ .../util/PackagePropertyFiles.java | 42 +++ .../util/PayloadSpecs.java | 117 +++++++ .../util/UpdateConfigs.java | 82 +++++ .../util/UpdateEngineErrorCodes.java | 84 +++++ .../util/UpdateEngineStatuses.java | 51 +++ sample_updater/tests/Android.mk | 32 ++ sample_updater/tests/AndroidManifest.xml | 31 ++ sample_updater/tests/build.properties | 1 + .../systemupdatersample/UpdateConfigTest.java | 79 +++++ .../ui/MainActivityTest.java | 48 +++ .../util/PayloadSpecsTest.java | 117 +++++++ .../util/UpdateConfigsTest.java | 63 ++++ 26 files changed, 1723 insertions(+), 97 deletions(-) create mode 100644 sample_updater/.gitignore create mode 100644 sample_updater/res/mipmap-hdpi/ic_launcher.png create mode 100644 sample_updater/res/mipmap-hdpi/ic_launcher_round.png create mode 100644 sample_updater/res/raw/sample.json create mode 100644 sample_updater/res/values/strings.xml delete mode 100644 sample_updater/src/com/android/update/ui/SystemUpdateActivity.java create mode 100644 sample_updater/src/com/example/android/systemupdatersample/PayloadSpec.java create mode 100644 sample_updater/src/com/example/android/systemupdatersample/UpdateConfig.java create mode 100644 sample_updater/src/com/example/android/systemupdatersample/ui/MainActivity.java create mode 100644 sample_updater/src/com/example/android/systemupdatersample/updates/AbNonStreamingUpdate.java create mode 100644 sample_updater/src/com/example/android/systemupdatersample/util/PackagePropertyFiles.java create mode 100644 sample_updater/src/com/example/android/systemupdatersample/util/PayloadSpecs.java create mode 100644 sample_updater/src/com/example/android/systemupdatersample/util/UpdateConfigs.java create mode 100644 sample_updater/src/com/example/android/systemupdatersample/util/UpdateEngineErrorCodes.java create mode 100644 sample_updater/src/com/example/android/systemupdatersample/util/UpdateEngineStatuses.java create mode 100644 sample_updater/tests/Android.mk create mode 100644 sample_updater/tests/AndroidManifest.xml create mode 100644 sample_updater/tests/build.properties create mode 100644 sample_updater/tests/src/com/example/android/systemupdatersample/UpdateConfigTest.java create mode 100644 sample_updater/tests/src/com/example/android/systemupdatersample/ui/MainActivityTest.java create mode 100644 sample_updater/tests/src/com/example/android/systemupdatersample/util/PayloadSpecsTest.java create mode 100644 sample_updater/tests/src/com/example/android/systemupdatersample/util/UpdateConfigsTest.java diff --git a/sample_updater/.gitignore b/sample_updater/.gitignore new file mode 100644 index 0000000000..487263f735 --- /dev/null +++ b/sample_updater/.gitignore @@ -0,0 +1,9 @@ +*~ +*.bak +*.pyc +*.pyc-2.4 +Thumbs.db +*.iml +.idea/ +gen/ +.vscode diff --git a/sample_updater/Android.mk b/sample_updater/Android.mk index 2b0fcbeec2..2786de44f9 100644 --- a/sample_updater/Android.mk +++ b/sample_updater/Android.mk @@ -15,13 +15,18 @@ # LOCAL_PATH := $(call my-dir) - include $(CLEAR_VARS) -LOCAL_PACKAGE_NAME := SystemUpdateApp +LOCAL_PACKAGE_NAME := SystemUpdaterSample LOCAL_SDK_VERSION := system_current -LOCAL_MODULE_TAGS := optional +LOCAL_MODULE_TAGS := samples + +# TODO: enable proguard and use proguard.flags file +LOCAL_PROGUARD_ENABLED := disabled LOCAL_SRC_FILES := $(call all-java-files-under, src) include $(BUILD_PACKAGE) + +# Use the following include to make our test apk. +include $(call all-makefiles-under,$(LOCAL_PATH)) diff --git a/sample_updater/AndroidManifest.xml b/sample_updater/AndroidManifest.xml index 66414b5d31..5bbb21c84f 100644 --- a/sample_updater/AndroidManifest.xml +++ b/sample_updater/AndroidManifest.xml @@ -15,17 +15,22 @@ --> + package="com.example.android.systemupdatersample"> - - - - - - - - + - + + + + + + + + + diff --git a/sample_updater/README.md b/sample_updater/README.md index a06c52d4bc..467ef5d4af 100644 --- a/sample_updater/README.md +++ b/sample_updater/README.md @@ -1 +1,72 @@ -# System update sample app. +# SystemUpdaterSample + +This app demonstrates how to use Android system updates APIs to install +[OTA updates](https://source.android.com/devices/tech/ota/). It contains a sample +client for `update_engine` to install A/B (seamless) updates and a sample of +applying non-A/B updates using `recovery`. + +A/B (seamless) update is available since Android Nougat (API 24), but this sample +targets the latest android. + + +## Running on a device + +The commands expected to be run from `$ANDROID_BUILD_TOP`. + +1. Compile the app `$ mmma bootable/recovery/sample_updater`. +2. Install the app to the device using `$ adb install `. +3. Add update config files. + + +## Update Config file + +Directory can be found in logs or on UI. Usually json config files are located in +`/data/user/0/com.example.android.systemupdatersample/files/configs/`. Example file +is located at `res/raw/sample.json`. + + +## Development + +- [x] Create a UI with list of configs, current version, + control buttons, progress bar and log viewer +- [x] Add `PayloadSpec` and `PayloadSpecs` for working with + update zip file +- [x] Add `UpdateConfig` for working with json config files +- [x] Add applying non-streaming update +- [ ] Add applying streaming update +- [ ] Prepare streaming update (partially downloading package) +- [ ] Add tests for `MainActivity` +- [ ] Add stop/reset the update +- [ ] Verify system partition checksum for package +- [ ] HAL compatibility check +- [ ] Change partition demo +- [ ] Add non-A/B updates demo + + +## Running tests + +1. Build `$ mmma bootable/recovery/sample_updater/` +2. Install app + `$ adb install $OUT/system/app/SystemUpdaterSample/SystemUpdaterSample.apk` +3. Install tests + `$ adb install $OUT/testcases/SystemUpdaterSampleTests/SystemUpdaterSampleTests.apk` +4. Run tests + `$ adb shell am instrument -w com.example.android.systemupdatersample.tests/android.support.test.runner.AndroidJUnitRunner` +5. Run a test file + ``` + $ adb shell am instrument \ + -w com.example.android.systemupdatersample.tests/android.support.test.runner.AndroidJUnitRunner \ + -c com.example.android.systemupdatersample.util.PayloadSpecsTest + ``` + + +## Getting access to `update_engine` API and read/write access to `/data` + +Run adb shell as a root, and set SELinux mode to permissive (0): + +```txt +$ adb root +$ adb shell +# setenforce 0 +# getenforce +``` diff --git a/sample_updater/res/layout/activity_main.xml b/sample_updater/res/layout/activity_main.xml index bd7d686771..3cd772107a 100644 --- a/sample_updater/res/layout/activity_main.xml +++ b/sample_updater/res/layout/activity_main.xml @@ -1,20 +1,163 @@ - + 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. +--> + android:orientation="vertical" + android:padding="4dip" + android:gravity="center_horizontal" + android:layout_width="fill_parent" + android:layout_height="fill_parent"> + + + + + + + + + + + + + + + + + + + +