Upgrade ota.sh to use new my own ota mechanism

This commit is contained in:
Pierre-Hugues Husson 2022-01-22 14:35:47 -05:00
parent fdc8be8692
commit 141780a2ac
4 changed files with 115 additions and 31 deletions

View File

@ -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

View File

@ -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",
],
}

90
cmds/phh-ota.cpp Normal file
View File

@ -0,0 +1,90 @@
#include <string>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/mount.h>
#include <stdio.h>
#include <unistd.h>
#include <libfiemap/image_manager.h>
#include <android-base/file.h>
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", &current_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", &current_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;
}

View File

@ -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