2022-01-22 19:35:47 +00:00
|
|
|
#include <string>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/mount.h>
|
2022-01-31 21:12:06 +00:00
|
|
|
#include <sys/sysmacros.h>
|
|
|
|
#include <sys/xattr.h>
|
2022-01-22 19:35:47 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
2022-01-31 21:12:06 +00:00
|
|
|
#include <fcntl.h>
|
2022-01-22 19:35:47 +00:00
|
|
|
#include <libfiemap/image_manager.h>
|
|
|
|
#include <android-base/file.h>
|
|
|
|
|
|
|
|
using namespace std::chrono_literals;
|
|
|
|
using namespace std::string_literals;
|
|
|
|
using android::fiemap::IImageManager;
|
|
|
|
|
2022-09-06 12:20:16 +00:00
|
|
|
|
|
|
|
std::string getNextSlot() {
|
2022-09-18 21:03:47 +00:00
|
|
|
std::string current_slot;
|
2022-09-06 12:20:16 +00:00
|
|
|
std::string next_slot = "a";
|
|
|
|
if(android::base::ReadFileToString("/metadata/phh/img", ¤t_slot) &&
|
|
|
|
current_slot.c_str()[0] == 'a') {
|
|
|
|
next_slot = "b";
|
|
|
|
}
|
|
|
|
return next_slot;
|
|
|
|
}
|
|
|
|
|
2022-01-22 19:35:47 +00:00
|
|
|
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) {
|
2022-09-06 12:20:16 +00:00
|
|
|
std::string next_slot = getNextSlot();
|
2022-01-22 19:35:47 +00:00
|
|
|
mkdir("/metadata/phh", 0700);
|
|
|
|
android::base::WriteStringToFile(next_slot, "/metadata/phh/img");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if(argc>=2 && strcmp(argv[1], "new-slot") == 0) {
|
2022-09-06 12:20:16 +00:00
|
|
|
std::string next_slot = getNextSlot();
|
2022-01-22 19:35:47 +00:00
|
|
|
|
|
|
|
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());
|
2022-01-31 21:12:06 +00:00
|
|
|
|
|
|
|
struct stat sb;
|
2022-04-24 12:03:48 +00:00
|
|
|
for(int i=0; i<10; i++) {
|
|
|
|
if(!stat(blockDev.c_str(), &sb)) break;
|
|
|
|
sleep(1);
|
|
|
|
}
|
2022-01-31 21:12:06 +00:00
|
|
|
|
|
|
|
if(!S_ISBLK(sb.st_mode)) {
|
|
|
|
fprintf(stderr, "blockDev wasn't block dev\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
unlink("/dev/phh-ota");
|
2022-02-02 00:22:27 +00:00
|
|
|
mknod("/dev/phh-ota", 0664 | S_IFBLK, makedev(major(sb.st_rdev), minor(sb.st_rdev)));
|
|
|
|
chmod("/dev/phh-ota", 0664);
|
2022-01-31 21:12:06 +00:00
|
|
|
// Allow system uid to write there
|
|
|
|
chown("/dev/phh-ota", 0, 1000);
|
2022-02-02 00:22:27 +00:00
|
|
|
const char *dstContext = "u:object_r:phhota_dev:s0";
|
2022-01-31 21:12:06 +00:00
|
|
|
setxattr("/dev/phh-ota", "security.selinux", dstContext, strlen(dstContext), 0);
|
|
|
|
|
2022-01-22 19:35:47 +00:00
|
|
|
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;
|
|
|
|
}
|