From 141780a2ac08098db9c9523121e16dc15d99a6a4 Mon Sep 17 00:00:00 2001 From: Pierre-Hugues Husson Date: Sat, 22 Jan 2022 14:35:47 -0500 Subject: [PATCH] Upgrade ota.sh to use new my own ota mechanism --- base.mk | 2 ++ cmds/Android.bp | 12 +++++++ cmds/phh-ota.cpp | 90 ++++++++++++++++++++++++++++++++++++++++++++++++ files/ota.sh | 42 ++++++---------------- 4 files changed, 115 insertions(+), 31 deletions(-) create mode 100644 cmds/phh-ota.cpp diff --git a/base.mk b/base.mk index 3a5ebc9..4a5c652 100644 --- a/base.mk +++ b/base.mk @@ -216,4 +216,6 @@ PRODUCT_COPY_FILES += \ device/phh/treble/proprietary-files/gome/fs16xx_01s_right.preset:system/phh/gome/fs16xx_01s_right.preset \ device/phh/treble/proprietary-files/umidigi/fs16xx_01s_mono.preset:system/phh/umidigi/fs16xx_01s_mono.preset +PRODUCT_PACKAGES += phh-ota + include build/make/target/product/gsi_release.mk diff --git a/cmds/Android.bp b/cmds/Android.bp index 4568089..9ff412a 100644 --- a/cmds/Android.bp +++ b/cmds/Android.bp @@ -204,3 +204,15 @@ cc_binary { "oplus-alert-slider.rc", ], } + +cc_binary { + name: "phh-ota", + srcs: [ + "phh-ota.cpp", + ], + shared_libs: [ + "libfs_mgr", + "libbase", + "liblp", + ], +} diff --git a/cmds/phh-ota.cpp b/cmds/phh-ota.cpp new file mode 100644 index 0000000..65aecbe --- /dev/null +++ b/cmds/phh-ota.cpp @@ -0,0 +1,90 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace std::chrono_literals; +using namespace std::string_literals; +using android::fiemap::IImageManager; + +int main(int argc, char **argv) { + mkdir("/metadata/gsi/phh", 0771); + chown("/metadata/gsi/phh", 0, 1000); + mkdir("/data/gsi/phh", 0771); + chown("/data/gsi/phh", 0, 1000); + + auto imgManager = IImageManager::Open("phh", 0ms); + if(argc>=2 && strcmp(argv[1], "unmap") == 0) { + fprintf(stderr, "Unmapping backing image returned %s\n", imgManager->UnmapImageDevice("system_otaphh_a") ? "true" : "false"); + fprintf(stderr, "Unmapping backing image returned %s\n", imgManager->UnmapImageDevice("system_otaphh_b") ? "true" : "false"); + return 0; + } + if(argc>=2 && strcmp(argv[1], "switch-slot") == 0) { + std::string current_slot; + std::string next_slot; + if(!android::base::ReadFileToString("/metadata/phh/img", ¤t_slot)) { + next_slot = "a"; + } else { + if(current_slot.c_str()[0] == 'a') + next_slot = "b"; + } + mkdir("/metadata/phh", 0700); + android::base::WriteStringToFile(next_slot, "/metadata/phh/img"); + return 0; + } + if(argc>=2 && strcmp(argv[1], "new-slot") == 0) { + std::string current_slot; + std::string next_slot; + if(!android::base::ReadFileToString("/metadata/phh/img", ¤t_slot)) { + next_slot = "a"; + } else { + if(current_slot.c_str()[0] == 'a') + next_slot = "b"; + } + + std::string imageName = "system_otaphh_"s + next_slot; + + fprintf(stderr, "Unmapping backing image returned %s\n", imgManager->UnmapImageDevice(imageName) ? "true" : "false"); + fprintf(stderr, "Deleting backing image returned %s\n", imgManager->DeleteBackingImage(imageName) ? "true" : "false"); + auto backRes = imgManager->CreateBackingImage(imageName, 4*1024*1024*1024LL, IImageManager::CREATE_IMAGE_DEFAULT, nullptr); + if(backRes.is_ok()) { + fprintf(stderr, "Creating system image succeeded\n"); + } else { + fprintf(stderr, "Creating system image failed\n"); + return -1; + } + + std::string blockDev; + fprintf(stderr, "Mapping backing image returned %s\n", imgManager->MapImageDevice(imageName, 0ms, &blockDev) ? "true" : "false"); + fprintf(stderr, "blockdev is %s\n", blockDev.c_str()); + printf("%s\n", blockDev.c_str()); + return 0; + } + if(argc>=2 && strcmp(argv[1], "delete-other-slot") == 0) { + const char* current_slot = getenv("PHH_OTA_SLOT"); + if(current_slot == NULL) { + imgManager->UnmapImageDevice("system_otaphh_a"); + imgManager->DeleteBackingImage("system_otaphh_a"); + imgManager->UnmapImageDevice("system_otaphh_b"); + imgManager->DeleteBackingImage("system_otaphh_b"); + return 0; + } + if(current_slot[0] == 'a') { + imgManager->UnmapImageDevice("system_otaphh_b"); + imgManager->DeleteBackingImage("system_otaphh_b"); + return 0; + } + if(current_slot[0] == 'b') { + imgManager->UnmapImageDevice("system_otaphh_a"); + imgManager->DeleteBackingImage("system_otaphh_a"); + return 0; + } + return 0; + } + + return 1; +} diff --git a/files/ota.sh b/files/ota.sh index ac72204..a43ce81 100644 --- a/files/ota.sh +++ b/files/ota.sh @@ -1,6 +1,6 @@ #!/system/bin/sh -set -e +set -ex if ! [ "$(getprop ro.boot.dynamic_partitions)" = true ];then echo "OTA is supported only for devices with dynamic partitions!" @@ -8,21 +8,24 @@ if ! [ "$(getprop ro.boot.dynamic_partitions)" = true ];then fi flavor=$(getprop ro.product.product.name) -nextVersion=$(curl --silent -L https://raw.githubusercontent.com/phhusson/treble_experimentations/master/ota/roar/$flavor/date) +if [ -f /system/phh/secure ];then + flavor=${flavor}-secure +fi +nextVersion=$(curl --silent -L https://raw.githubusercontent.com/phhusson/treble_experimentations/master/ota/squeak/$flavor/date) if [ -z "$nextVersion" ];then echo "Couldn't find any OTA for $flavor" exit 1 fi -url=$(curl --silent -L https://raw.githubusercontent.com/phhusson/treble_experimentations/master/ota/roar/$flavor/url) -size=$(curl --silent -L https://raw.githubusercontent.com/phhusson/treble_experimentations/master/ota/roar/$flavor/size) +url=$(curl --silent -L https://raw.githubusercontent.com/phhusson/treble_experimentations/master/ota/squeak/$flavor/url) +size=$(curl --silent -L https://raw.githubusercontent.com/phhusson/treble_experimentations/master/ota/squeak/$flavor/size) if [ "$(getprop ro.product.build.date.utc)" = "$nextVersion" ];then echo "Installing $nextVersion onto itself, aborting" exit 1 fi -if ! curl --silent -L https://raw.githubusercontent.com/phhusson/treble_experimentations/master/ota/roar/$flavor/known_releases |grep -q $(getprop ro.product.build.date.utc);then +if ! curl --silent -L https://raw.githubusercontent.com/phhusson/treble_experimentations/master/ota/squeak/$flavor/known_releases |grep -q $(getprop ro.product.build.date.utc);then echo "Warning! The build you are currently running is unknown. Type YES to confirm you want to apply OTA from $url" read answer if ! [ "$answer" = YES ];then @@ -39,34 +42,11 @@ if [ -b /dev/tmp-phh ] && ! tune2fs -l /dev/tmp-phh |grep 'Last mount time' |gr fi fi -lptools remove system_phh -free=$(lptools free |grep -oE '[0-9]+$') -if [ "$free" -le "$size" ];then - echo "Warning! There doesn't seem to be enough space on super partition." - echo "Do you want me to try to make more space? Type YES" - read answer - if ! [ "$answer" = YES ];then - exit 1 - fi - lptools clear-cow || true - lptools unlimited-group || true - lptools remove product || true - lptools remove product$(getprop ro.boot.slot_suffix) || true - free=$(lptools free |grep -oE '[0-9]+$') - if [ "$free" -le "$size" ];then - echo "Sorry, there is still not enough space available. OTA requires $size, you have $free available" - exit 1 - fi -fi - - -lptools create system_phh "$size" -lptools unmap system_phh -dmDevice=$(lptools map system_phh|grep -oE '/dev/block/[^ ]*') echo "Flashing from ${url}..." -curl -L "$url" | busybox_phh xz -d -c | simg2img_simple > $dmDevice +dmDevice=$(phh-ota new-slot) +curl -L "$url" | busybox_phh xz -d -c > $dmDevice +phh-ota switch-slot -lptools replace system_phh system reboot exit 0