diff --git a/BoardConfig.mk b/BoardConfig.mk index 4960531..7b70727 100644 --- a/BoardConfig.mk +++ b/BoardConfig.mk @@ -96,6 +96,7 @@ BOARD_USES_QCOM_HARDWARE := true TARGET_RECOVERY_PIXEL_FORMAT := "RGBX_8888" # Releasetools +TARGET_RECOVERY_UPDATER_LIBS := librecovery_updater_xiaomi TARGET_RELEASETOOLS_EXTENSIONS := $(DEVICE_PATH) # Sepolicy diff --git a/board-info.txt b/board-info.txt new file mode 100644 index 0000000..9bcd624 --- /dev/null +++ b/board-info.txt @@ -0,0 +1 @@ +require version-baseband=4.3.c5-00029,V11.0.4.0 diff --git a/device.mk b/device.mk index 1f3bcbc..8edd8ce 100644 --- a/device.mk +++ b/device.mk @@ -43,6 +43,10 @@ PRODUCT_COPY_FILES += \ PRODUCT_COPY_FILES += \ $(LOCAL_PATH)/configs/permissions/qti_whitelist.xml:$(TARGET_COPY_OUT_SYSTEM)/etc/sysconfig/qti_whitelist.xml +# Recovery +PRODUCT_PACKAGES += \ + librecovery_updater_xiaomi + # Screen density PRODUCT_AAPT_CONFIG := normal PRODUCT_AAPT_PREF_CONFIG := xxhdpi diff --git a/recovery/Android.mk b/recovery/Android.mk new file mode 100644 index 0000000..ff84438 --- /dev/null +++ b/recovery/Android.mk @@ -0,0 +1,10 @@ +LOCAL_PATH := $(call my-dir) + +include $(CLEAR_VARS) +LOCAL_C_INCLUDES := \ + bootable/recovery \ + bootable/recovery/edify/include \ + bootable/recovery/otautil/include +LOCAL_SRC_FILES := recovery_updater.cpp +LOCAL_MODULE := librecovery_updater_xiaomi +include $(BUILD_STATIC_LIBRARY) diff --git a/recovery/recovery_updater.cpp b/recovery/recovery_updater.cpp new file mode 100644 index 0000000..a785ad6 --- /dev/null +++ b/recovery/recovery_updater.cpp @@ -0,0 +1,193 @@ +/* + * Copyright (C) 2020, The LineageOS 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 + +#include +#include + +#include "edify/expr.h" +#include "otautil/error_code.h" + +#define MAX(a, b) (((a) > (b)) ? (a) : (b)) + +#define ALPHABET_LEN 256 + +#define BASEBAND_PART_PATH "/dev/block/bootdevice/by-name/modem" +#define BASEBAND_VER_STR_START "QC_IMAGE_VERSION_STRING=MPSS.AT." +#define BASEBAND_VER_STR_START_LEN 32 +#define BASEBAND_VER_BUF_LEN 255 + +/* Boyer-Moore string search implementation from Wikipedia */ + +/* Return longest suffix length of suffix ending at str[p] */ +static int max_suffix_len(const char* str, size_t str_len, size_t p) { + uint32_t i; + + for (i = 0; (str[p - i] == str[str_len - 1 - i]) && (i < p);) { + i++; + } + + return i; +} + +/* Generate table of distance between last character of pat and rightmost + * occurrence of character c in pat + */ +static void bm_make_delta1(int* delta1, const char* pat, size_t pat_len) { + uint32_t i; + for (i = 0; i < ALPHABET_LEN; i++) { + delta1[i] = pat_len; + } + for (i = 0; i < pat_len - 1; i++) { + uint8_t idx = (uint8_t)pat[i]; + delta1[idx] = pat_len - 1 - i; + } +} + +/* Generate table of next possible full match from mismatch at pat[p] */ +static void bm_make_delta2(int* delta2, const char* pat, size_t pat_len) { + int p; + uint32_t last_prefix = pat_len - 1; + + for (p = pat_len - 1; p >= 0; p--) { + /* Compare whether pat[p-pat_len] is suffix of pat */ + if (strncmp(pat + p, pat, pat_len - p) == 0) { + last_prefix = p + 1; + } + delta2[p] = last_prefix + (pat_len - 1 - p); + } + + for (p = 0; p < (int)pat_len - 1; p++) { + /* Get longest suffix of pattern ending on character pat[p] */ + int suf_len = max_suffix_len(pat, pat_len, p); + if (pat[p - suf_len] != pat[pat_len - 1 - suf_len]) { + delta2[pat_len - 1 - suf_len] = pat_len - 1 - p + suf_len; + } + } +} + +static char* bm_search(const char* str, size_t str_len, const char* pat, size_t pat_len) { + int delta1[ALPHABET_LEN]; + int delta2[pat_len]; + int i; + + bm_make_delta1(delta1, pat, pat_len); + bm_make_delta2(delta2, pat, pat_len); + + if (pat_len == 0) { + return (char*)str; + } + + i = pat_len - 1; + while (i < (int)str_len) { + int j = pat_len - 1; + while (j >= 0 && (str[i] == pat[j])) { + i--; + j--; + } + if (j < 0) { + return (char*)(str + i + 1); + } + i += MAX(delta1[(uint8_t)str[i]], delta2[j]); + } + + return NULL; +} + +static int get_baseband_version(char *ver_str, size_t len) { + int ret = 0; + int fd; + int baseband_size; + char *baseband_data = NULL; + char *offset = NULL; + + fd = open(BASEBAND_PART_PATH, O_RDONLY); + if (fd < 0) { + ret = errno; + goto err_ret; + } + + baseband_size = lseek64(fd, 0, SEEK_END); + if (baseband_size == -1) { + ret = errno; + goto err_fd_close; + } + + baseband_data = (char *) mmap(NULL, baseband_size, PROT_READ, MAP_PRIVATE, fd, 0); + if (baseband_data == (char *)-1) { + ret = errno; + goto err_fd_close; + } + + /* Do Boyer-Moore search across BASEBAND data */ + offset = bm_search(baseband_data, baseband_size, BASEBAND_VER_STR_START, + BASEBAND_VER_STR_START_LEN); + if (offset != NULL) { + strncpy(ver_str, offset + BASEBAND_VER_STR_START_LEN, len); + } else { + ret = -ENOENT; + } + + munmap(baseband_data, baseband_size); +err_fd_close: + close(fd); +err_ret: + return ret; +} + +/* verify_baseband("BASEBAND_VERSION", "BASEBAND_VERSION", ...) */ +Value * VerifyBasebandFn(const char *name, State *state, + const std::vector>& argv) { + char current_baseband_version[BASEBAND_VER_BUF_LEN]; + int ret; + + ret = get_baseband_version(current_baseband_version, BASEBAND_VER_BUF_LEN); + if (ret) { + return ErrorAbort(state, kFreadFailure, "%s() failed to read current baseband version: %d", + name, ret); + } + + std::vector args; + if (!ReadArgs(state, argv, &args)) { + return ErrorAbort(state, kArgsParsingFailure, "%s() error parsing arguments", name); + } + + ret = 0; + for (auto &baseband_version : args) { + if (strncmp(baseband_version.c_str(), current_baseband_version, + baseband_version.length()) <= 0) { + ret = 1; + break; + } + } + + return StringValue(strdup(ret ? "1" : "0")); +} + +void Register_librecovery_updater_xiaomi() { + RegisterFunction("xiaomi.verify_baseband", VerifyBasebandFn); +} diff --git a/releasetools.py b/releasetools.py index 6831e15..59096f9 100644 --- a/releasetools.py +++ b/releasetools.py @@ -17,6 +17,14 @@ import common import re +def FullOTA_Assertions(info): + AddBasebandAssertion(info, info.input_zip) + return + +def IncrementalOTA_Assertions(info): + AddBasebandAssertion(info, info.input_zip) + return + def FullOTA_InstallEnd(info): OTA_InstallEnd(info) return @@ -36,3 +44,15 @@ def OTA_InstallEnd(info): AddImage(info, "dtbo.img", "/dev/block/bootdevice/by-name/dtbo") AddImage(info, "vbmeta.img", "/dev/block/bootdevice/by-name/vbmeta") return + +def AddBasebandAssertion(info, input_zip): + android_info = input_zip.read("OTA/android-info.txt") + m = re.search(r'require\s+version-baseband\s*=\s*(.+)', android_info) + if m: + timestamp, firmware_version = m.group(1).rstrip().split(',') + timestamps = timestamp.split('|') + if ((len(timestamps) and '*' not in timestamps) and \ + (len(firmware_version) and '*' not in firmware_version)): + cmd = 'assert(xiaomi.verify_baseband(' + ','.join(['"%s"' % baseband for baseband in timestamps]) + ') == "1" || abort("ERROR: This package requires firmware from MIUI {1} or newer. Please upgrade firmware and retry!"););' + info.script.AppendExtra(cmd.format(timestamps, firmware_version)) + return