diff --git a/BoardConfig.mk b/BoardConfig.mk index 624d25f..1efa38a 100644 --- a/BoardConfig.mk +++ b/BoardConfig.mk @@ -83,8 +83,8 @@ ODM_MANIFEST_SKUS += nfc ODM_MANIFEST_NFC_FILES := $(DEVICE_PATH)/manifest_nfc.xml # Init -TARGET_INIT_VENDOR_LIB := //$(DEVICE_PATH):libinit_xiaomi_rosemary -TARGET_RECOVERY_DEVICE_MODULES := libinit_xiaomi_rosemary +TARGET_INIT_VENDOR_LIB := //$(DEVICE_PATH):init_rosemary +TARGET_RECOVERY_DEVICE_MODULES := init_rosemary # Kernel TARGET_KERNEL_ARCH := arm64 diff --git a/init/Android.bp b/init/Android.bp deleted file mode 100644 index 5eefe75..0000000 --- a/init/Android.bp +++ /dev/null @@ -1,30 +0,0 @@ -// -// 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. -// - -cc_library_static { - name: "libinit_xiaomi_rosemary", - recovery_available: true, - srcs: [ - "init.cpp" - ], - include_dirs: [ - "system/libbase/include", - "system/core/init" - ], - shared_libs: [ - "libbase", - ] -} diff --git a/init/init.cpp b/init/init.cpp deleted file mode 100644 index af118c4..0000000 --- a/init/init.cpp +++ /dev/null @@ -1,85 +0,0 @@ -/* - * 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 - -#define _REALLY_INCLUDE_SYS__SYSTEM_PROPERTIES_H_ -#include - -#include "property_service.h" - -void property_override(char const prop[], char const value[]) -{ - auto pi = (prop_info *) __system_property_find(prop); - - if (pi != nullptr) { - __system_property_update(pi, value, strlen(value)); - } else { - __system_property_add(prop, strlen(prop), value, strlen(value)); - } -} - -void load_dalvik_properties() -{ - char const *heapstartsize; - char const *heapgrowthlimit; - char const *heapsize; - char const *heapminfree; - char const *heapmaxfree; - char const *heaptargetutilization; - struct sysinfo sys; - - sysinfo(&sys); - - if (sys.totalram >= 5ull * 1024 * 1024 * 1024) { - // from - phone-xhdpi-6144-dalvik-heap.mk - heapstartsize = "16m"; - heapgrowthlimit = "256m"; - heapsize = "512m"; - heaptargetutilization = "0.5"; - heapminfree = "8m"; - heapmaxfree = "32m"; - } else if (sys.totalram >= 3ull * 1024 * 1024 * 1024) { - // from - phone-xhdpi-4096-dalvik-heap.mk - heapstartsize = "8m"; - heapgrowthlimit = "192m"; - heapsize = "512m"; - heaptargetutilization = "0.6"; - heapminfree = "8m"; - heapmaxfree = "16m"; - } else { - // from - phone-xhdpi-2048-dalvik-heap.mk - heapstartsize = "8m"; - heapgrowthlimit = "192m"; - heapsize = "512m"; - heaptargetutilization = "0.75"; - heapminfree = "512k"; - heapmaxfree = "8m"; - } - - property_override("dalvik.vm.heapstartsize", heapstartsize); - property_override("dalvik.vm.heapgrowthlimit", heapgrowthlimit); - property_override("dalvik.vm.heapsize", heapsize); - property_override("dalvik.vm.heaptargetutilization", heaptargetutilization); - property_override("dalvik.vm.heapminfree", heapminfree); - property_override("dalvik.vm.heapmaxfree", heapmaxfree); -} - -void vendor_load_properties() -{ - load_dalvik_properties(); -} diff --git a/libinit/Android.bp b/libinit/Android.bp new file mode 100644 index 0000000..b685096 --- /dev/null +++ b/libinit/Android.bp @@ -0,0 +1,25 @@ +// +// Copyright (C) 2021 The LineageOS Project +// +// SPDX-License-Identifier: Apache-2.0 +// + +cc_library_static { + name: "libinit_rosemary", + srcs: [ + "libinit_dalvik_heap.cpp", + "libinit_variant.cpp", + "libinit_utils.cpp", + ], + whole_static_libs: ["libbase"], + export_include_dirs: ["include"], + recovery_available: true, +} + +cc_library_static { + name: "init_rosemary", + srcs: ["init_rosemary.cpp"], + whole_static_libs: ["libinit_rosemary"], + include_dirs: ["system/core/init"], + recovery_available: true, +} diff --git a/libinit/include/libinit_dalvik_heap.h b/libinit/include/libinit_dalvik_heap.h new file mode 100644 index 0000000..75144b2 --- /dev/null +++ b/libinit/include/libinit_dalvik_heap.h @@ -0,0 +1,23 @@ +/* + * Copyright (C) 2021 The LineageOS Project + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#ifndef LIBINIT_DALVIK_HEAP_H +#define LIBINIT_DALVIK_HEAP_H + +#include + +typedef struct dalvik_heap_info { + std::string heapstartsize; + std::string heapgrowthlimit; + std::string heapsize; + std::string heapminfree; + std::string heapmaxfree; + std::string heaptargetutilization; +} dalvik_heap_info_t; + +void set_dalvik_heap(void); + +#endif // LIBINIT_DALVIK_HEAP_H diff --git a/libinit/include/libinit_utils.h b/libinit/include/libinit_utils.h new file mode 100644 index 0000000..dc023d3 --- /dev/null +++ b/libinit/include/libinit_utils.h @@ -0,0 +1,18 @@ +/* + * Copyright (C) 2021 The LineageOS Project + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#ifndef LIBINIT_UTILS_H +#define LIBINIT_UTILS_H + +#include + +void property_override(std::string prop, std::string value, bool add = true); + +void set_ro_build_prop(const std::string &prop, const std::string &value, bool product = false); + +std::string fingerprint_to_description(std::string fingerprint); + +#endif // LIBINIT_UTILS_H diff --git a/libinit/include/libinit_variant.h b/libinit/include/libinit_variant.h new file mode 100644 index 0000000..ba51bb0 --- /dev/null +++ b/libinit/include/libinit_variant.h @@ -0,0 +1,30 @@ +/* + * Copyright (C) 2021 The LineageOS Project + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#ifndef LIBINIT_VARIANT_H +#define LIBINIT_VARIANT_H + +#include +#include + +typedef struct variant_info { + std::string hwc_value; + std::string sku_value; + + std::string brand; + std::string device; + std::string marketname; + std::string model; + std::string build_fingerprint; + + bool nfc; +} variant_info_t; + +void search_variant(const std::vector variants); + +void set_variant_props(const variant_info_t variant); + +#endif // LIBINIT_VARIANT_H diff --git a/libinit/init_rosemary.cpp b/libinit/init_rosemary.cpp new file mode 100644 index 0000000..a6da65e --- /dev/null +++ b/libinit/init_rosemary.cpp @@ -0,0 +1,34 @@ +/* + * Copyright (C) 2021 The LineageOS Project + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include +#include + +#include "vendor_init.h" + +static const variant_info_t rosemary_info = { + .hwc_value = "Global", + .sku_value = "", + .device = "rosemary", + .nfc = true, +}; + +static const variant_info_t secret_info = { + .hwc_value = "Global_PA", + .sku_value = "", + .device = "secret", + .nfc = false, +}; + +static const std::vector variants = { + rosemary_info, + secret_info, +}; + +void vendor_load_properties() { + search_variant(variants); + set_dalvik_heap(); +} diff --git a/libinit/libinit_dalvik_heap.cpp b/libinit/libinit_dalvik_heap.cpp new file mode 100644 index 0000000..ba5992e --- /dev/null +++ b/libinit/libinit_dalvik_heap.cpp @@ -0,0 +1,67 @@ +/* + * Copyright (C) 2021 The LineageOS Project + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include +#include + +#include + +#define HEAPSTARTSIZE_PROP "dalvik.vm.heapstartsize" +#define HEAPGROWTHLIMIT_PROP "dalvik.vm.heapgrowthlimit" +#define HEAPSIZE_PROP "dalvik.vm.heapsize" +#define HEAPMINFREE_PROP "dalvik.vm.heapminfree" +#define HEAPMAXFREE_PROP "dalvik.vm.heapmaxfree" +#define HEAPTARGETUTILIZATION_PROP "dalvik.vm.heaptargetutilization" + +#define GB(b) (b * 1024ull * 1024 * 1024) + +static const dalvik_heap_info_t dalvik_heap_info_6144 = { + .heapstartsize = "16m", + .heapgrowthlimit = "256m", + .heapsize = "512m", + .heapminfree = "8m", + .heapmaxfree = "32m", + .heaptargetutilization = "0.5", +}; + +static const dalvik_heap_info_t dalvik_heap_info_4096 = { + .heapstartsize = "8m", + .heapgrowthlimit = "256m", + .heapsize = "512m", + .heapminfree = "8m", + .heapmaxfree = "16m", + .heaptargetutilization = "0.6", +}; + +static const dalvik_heap_info_t dalvik_heap_info_2048 = { + .heapstartsize = "8m", + .heapgrowthlimit = "192m", + .heapsize = "512m", + .heapminfree = "512k", + .heapmaxfree = "8m", + .heaptargetutilization = "0.75", +}; + +void set_dalvik_heap() { + struct sysinfo sys; + const dalvik_heap_info_t *dhi; + + sysinfo(&sys); + + if (sys.totalram > GB(5)) + dhi = &dalvik_heap_info_6144; + else if (sys.totalram > GB(3)) + dhi = &dalvik_heap_info_4096; + else + dhi = &dalvik_heap_info_2048; + + property_override(HEAPSTARTSIZE_PROP, dhi->heapstartsize); + property_override(HEAPGROWTHLIMIT_PROP, dhi->heapgrowthlimit); + property_override(HEAPSIZE_PROP, dhi->heapsize); + property_override(HEAPTARGETUTILIZATION_PROP, dhi->heaptargetutilization); + property_override(HEAPMINFREE_PROP, dhi->heapminfree); + property_override(HEAPMAXFREE_PROP, dhi->heapmaxfree); +} diff --git a/libinit/libinit_utils.cpp b/libinit/libinit_utils.cpp new file mode 100644 index 0000000..1bac383 --- /dev/null +++ b/libinit/libinit_utils.cpp @@ -0,0 +1,75 @@ +/* + * Copyright (C) 2021 The LineageOS Project + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#define _REALLY_INCLUDE_SYS__SYSTEM_PROPERTIES_H_ +#include +#include + +#include + +void property_override(std::string prop, std::string value, bool add) { + auto pi = (prop_info *) __system_property_find(prop.c_str()); + if (pi != nullptr) { + __system_property_update(pi, value.c_str(), value.length()); + } else if (add) { + __system_property_add(prop.c_str(), prop.length(), value.c_str(), value.length()); + } +} + +std::vector ro_props_default_source_order = { + "odm.", + "odm_dlkm.", + "product.", + "system.", + "system_ext.", + "vendor.", + "vendor_dlkm.", + "", +}; + +void set_ro_build_prop(const std::string &prop, const std::string &value, bool product) { + std::string prop_name; + + for (const auto &source : ro_props_default_source_order) { + if (product) + prop_name = "ro.product." + source + prop; + else + prop_name = "ro." + source + "build." + prop; + + property_override(prop_name, value, true); + } +} + +#define FIND_AND_REMOVE(s, delimiter, variable_name) \ + std::string variable_name = s.substr(0, s.find(delimiter)); \ + s.erase(0, s.find(delimiter) + delimiter.length()); + +#define APPEND_STRING(s, to_append) \ + s.append(" "); \ + s.append(to_append); + +std::string fingerprint_to_description(std::string fingerprint) { + std::string delimiter = "/"; + std::string delimiter2 = ":"; + std::string build_fingerprint_copy = fingerprint; + + FIND_AND_REMOVE(build_fingerprint_copy, delimiter, brand) + FIND_AND_REMOVE(build_fingerprint_copy, delimiter, product) + FIND_AND_REMOVE(build_fingerprint_copy, delimiter2, device) + FIND_AND_REMOVE(build_fingerprint_copy, delimiter, platform_version) + FIND_AND_REMOVE(build_fingerprint_copy, delimiter, build_id) + FIND_AND_REMOVE(build_fingerprint_copy, delimiter2, build_number) + FIND_AND_REMOVE(build_fingerprint_copy, delimiter, build_variant) + std::string build_version_tags = build_fingerprint_copy; + + std::string description = product + "-" + build_variant; + APPEND_STRING(description, platform_version) + APPEND_STRING(description, build_id) + APPEND_STRING(description, build_number) + APPEND_STRING(description, build_version_tags) + + return description; +} diff --git a/libinit/libinit_variant.cpp b/libinit/libinit_variant.cpp new file mode 100644 index 0000000..8324def --- /dev/null +++ b/libinit/libinit_variant.cpp @@ -0,0 +1,45 @@ +/* + * Copyright (C) 2021 The LineageOS Project + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include +#include + +#include + +using android::base::GetProperty; + +#define HWC_PROP "ro.boot.hwc" +#define SKU_PROP "ro.boot.product.hardware.sku" + +void search_variant(const std::vector variants) { + std::string hwc_value = GetProperty(HWC_PROP, ""); + std::string sku_value = GetProperty(SKU_PROP, ""); + + for (const auto& variant : variants) { + if ((variant.hwc_value == "" || variant.hwc_value == hwc_value) && + (variant.sku_value == "" || variant.sku_value == sku_value)) { + set_variant_props(variant); + break; + } + } +} + +void set_variant_props(const variant_info_t variant) { + set_ro_build_prop("brand", variant.brand, true); + set_ro_build_prop("device", variant.device, true); + set_ro_build_prop("marketname", variant.marketname, true); + set_ro_build_prop("model", variant.model, true); + + set_ro_build_prop("fingerprint", variant.build_fingerprint); + property_override("ro.bootimage.build.fingerprint", variant.build_fingerprint); + + property_override("ro.build.description", fingerprint_to_description(variant.build_fingerprint)); + + property_override("ro.boot.hardware.sku", variant.device); + + if (variant.nfc) + property_override(SKU_PROP, "nfc"); +}