From 57bed6d8c9beace01b743580d6c878b0724d2f90 Mon Sep 17 00:00:00 2001 From: Tianjie Xu Date: Tue, 15 Dec 2015 11:47:30 -0800 Subject: updater: Add a function to check first block Add and register a function to check if the device has been remounted since last update during incremental OTA. This function reads block 0 and executes before partition recovery for version >= 4. Bug: 21124327 Change-Id: I8b915b9f1d4736b3609daa9d16bd123225be357f (cherry picked from commit 30bf4765593e639966df9f460df22c3fe912e7bf) --- updater/blockimg.cpp | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++++ updater/install.h | 3 +++ 2 files changed, 58 insertions(+) diff --git a/updater/blockimg.cpp b/updater/blockimg.cpp index 3b26f057a..c6daf7db5 100644 --- a/updater/blockimg.cpp +++ b/updater/blockimg.cpp @@ -42,6 +42,7 @@ #include "applypatch/applypatch.h" #include "edify/expr.h" +#include "install.h" #include "mincrypt/sha.h" #include "minzip/Hash.h" #include "print_sha1.h" @@ -1650,6 +1651,59 @@ Value* RangeSha1Fn(const char* name, State* state, int /* argc */, Expr* argv[]) return StringValue(strdup(print_sha1(digest).c_str())); } +// This function checks if a device has been remounted R/W prior to an incremental +// OTA update. This is an common cause of update abortion. The function reads the +// 1st block of each partition and check for mounting time/count. It return string "t" +// if executes successfully and an empty string otherwise. + +Value* CheckFirstBlockFn(const char* name, State* state, int argc, Expr* argv[]) { + Value* arg_filename; + + if (ReadValueArgs(state, argv, 1, &arg_filename) < 0) { + return nullptr; + } + std::unique_ptr filename(arg_filename, FreeValue); + + if (filename->type != VAL_STRING) { + ErrorAbort(state, "filename argument to %s must be string", name); + return StringValue(strdup("")); + } + + int fd = open(arg_filename->data, O_RDONLY); + unique_fd fd_holder(fd); + if (fd == -1) { + ErrorAbort(state, "open \"%s\" failed: %s", arg_filename->data, strerror(errno)); + return StringValue(strdup("")); + } + + RangeSet blk0 {1 /*count*/, 1/*size*/, std::vector {0, 1}/*position*/}; + std::vector block0_buffer(BLOCKSIZE); + + if (ReadBlocks(blk0, block0_buffer, fd) == -1) { + ErrorAbort(state, "failed to read %s: %s", arg_filename->data, + strerror(errno)); + return StringValue(strdup("")); + } + + // https://ext4.wiki.kernel.org/index.php/Ext4_Disk_Layout + // Super block starts from block 0, offset 0x400 + // 0x2C: len32 Mount time + // 0x30: len32 Write time + // 0x34: len16 Number of mounts since the last fsck + // 0x38: len16 Magic signature 0xEF53 + + time_t mount_time = *reinterpret_cast(&block0_buffer[0x400+0x2C]); + uint16_t mount_count = *reinterpret_cast(&block0_buffer[0x400+0x34]); + + if (mount_count > 0) { + uiPrintf(state, "Device was remounted R/W %d times\n", mount_count); + uiPrintf(state, "Last remount happened on %s", ctime(&mount_time)); + } + + return StringValue(strdup("t")); +} + + Value* BlockImageRecoverFn(const char* name, State* state, int argc, Expr* argv[]) { Value* arg_filename; Value* arg_ranges; @@ -1731,5 +1785,6 @@ void RegisterBlockImageFunctions() { RegisterFunction("block_image_verify", BlockImageVerifyFn); RegisterFunction("block_image_update", BlockImageUpdateFn); RegisterFunction("block_image_recover", BlockImageRecoverFn); + RegisterFunction("check_first_block", CheckFirstBlockFn); RegisterFunction("range_sha1", RangeSha1Fn); } diff --git a/updater/install.h b/updater/install.h index 659c8b41c..70e343404 100644 --- a/updater/install.h +++ b/updater/install.h @@ -19,6 +19,9 @@ void RegisterInstallFunctions(); +// uiPrintf function prints msg to screen as well as logs +void uiPrintf(State* state, const char* format, ...); + static int make_parents(char* name); #endif -- cgit v1.2.3