From 99705d1f4f542dbfc4d8ca1ca9a34c73ee5ddb72 Mon Sep 17 00:00:00 2001 From: JPyke3 Date: Wed, 23 Sep 2026 20:09:51 +1000 Subject: [PATCH 1/2] feat(armada): add Android DT matching for AYN Odin 3 --- README.md | 1 + src/device.c | 69 +++++++++++++++++++++++++++++++++++++++------------- 2 files changed, 53 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index 941b6bb..543a19b 100644 --- a/README.md +++ b/README.md @@ -27,6 +27,7 @@ attempts to simplify running Linux-based or other OS that use DT by providing: - ASUS Zenbook A14 UX3407QA - ASUS Zenbook A14 UX3407RA - * Asus Zenbook A16 UX3607OA* +- AYN Odin 3 - AYN Thor - Dell Inspiron 14 Plus 7441 - * Dell Latitude 5455* diff --git a/src/device.c b/src/device.c index 5a3c745..afd8e11 100644 --- a/src/device.c +++ b/src/device.c @@ -14,14 +14,39 @@ __declspec(allocate(".devs$a")) struct device *__start_dtbloader_dev = NULL; __declspec(allocate(".devs$d")) struct device *__stop_dtbloader_dev = NULL; -static struct device armada_retroid_pocket_nova = { - .name = L"Retroid Pocket Nova", - .dtb = L"qcom\\qcs8550-retroidpocket-rpnova.dtb", +struct armada_device_match { + struct device device; + const char *android_compatible; + const char *primary_panel; + const char *secondary_panel; }; -static struct device armada_ayn_thor = { - .name = L"AYN Thor", - .dtb = L"qcom\\qcs8550-ayn-thor.dtb", +static struct armada_device_match armada_devices[] = { + { + .device = { + .name = L"Retroid Pocket Nova", + .dtb = L"qcom\\qcs8550-retroidpocket-rpnova.dtb", + }, + .android_compatible = "qcom,kalamap-hdk", + .primary_panel = "il97680a amoled panel without DSC", + }, + { + .device = { + .name = L"AYN Thor", + .dtb = L"qcom\\qcs8550-ayn-thor.dtb", + }, + .android_compatible = "qcom,kalamap-hdk", + .primary_panel = "icna3520 amoled panel with DSC", + .secondary_panel = "ch13726a video mode dsi boe panel with DSC", + }, + { + .device = { + .name = L"AYN Odin 3", + .dtb = L"qcom\\cq8725s-ayn-odin3.dtb", + }, + .android_compatible = "qcom,sunp-hdk", + .primary_panel = "icna3520 amoled panel with DSC", + }, }; static bool panel_name_is(void *dtb, const char *display_path, const char *expected) @@ -49,7 +74,9 @@ static bool panel_name_is(void *dtb, const char *display_path, const char *expec static struct device *match_armada_device(void) { EFI_GUID dtb_table_guid = EFI_DTB_TABLE_GUID; + struct device *match = NULL; void *android_dtb; + unsigned i; if (EFI_ERROR(LibGetSystemConfigurationTable(&dtb_table_guid, &android_dtb))) return NULL; @@ -57,20 +84,28 @@ static struct device *match_armada_device(void) if (fdt_check_header(android_dtb)) return NULL; - if (fdt_node_check_compatible(android_dtb, 0, "qcom,kalamap-hdk")) - return NULL; + for (i = 0; i < ARRAY_SIZE(armada_devices); ++i) { + struct armada_device_match *dev = &armada_devices[i]; - if (panel_name_is(android_dtb, "/soc/qcom,dsi-display-primary", - "il97680a amoled panel without DSC")) - return &armada_retroid_pocket_nova; + if (fdt_node_check_compatible(android_dtb, 0, dev->android_compatible)) + continue; + + if (!panel_name_is(android_dtb, "/soc/qcom,dsi-display-primary", + dev->primary_panel)) + continue; - if (panel_name_is(android_dtb, "/soc/qcom,dsi-display-primary", - "icna3520 amoled panel with DSC") && - panel_name_is(android_dtb, "/soc/qcom,dsi-display-secondary", - "ch13726a video mode dsi boe panel with DSC")) - return &armada_ayn_thor; + if (dev->secondary_panel && + !panel_name_is(android_dtb, "/soc/qcom,dsi-display-secondary", + dev->secondary_panel)) + continue; + + if (match) + return NULL; + + match = &dev->device; + } - return NULL; + return match; } From 28d1c54a04339eb8338f1ba70aa94a46966fd351 Mon Sep 17 00:00:00 2001 From: JPyke3 Date: Thu, 24 Sep 2026 19:45:25 +1000 Subject: [PATCH 2/2] feat(armada): detect devices from active Android DTBO --- scripts/describe_android_dt.sh | 4 +- src/device.c | 121 +++++++++++++++++++++++++++++---- src/include/device.h | 1 + src/qcom.c | 107 +++++++++++++++++++++++++++++ 4 files changed, 218 insertions(+), 15 deletions(-) diff --git a/scripts/describe_android_dt.sh b/scripts/describe_android_dt.sh index 1378927..6a7e1f4 100755 --- a/scripts/describe_android_dt.sh +++ b/scripts/describe_android_dt.sh @@ -18,4 +18,6 @@ find /sys/firmware/devicetree/base/soc/qcom,mdss_mdp@* -maxdepth 2 -type f -name if [ -r /sys/firmware/devicetree/base/soc/qcom,dsi-display-secondary/qcom,dsi-default-panel ]; then printf 'secondary-panel-phandle:' >> /sdcard/adtbloader/identity.txt; od -An -t x1 /sys/firmware/devicetree/base/soc/qcom,dsi-display-secondary/qcom,dsi-default-panel >> /sdcard/adtbloader/identity.txt; fi if [ -r /sys/firmware/devicetree/base/soc/qcom,dsi-display-secondary/qcom,dsi-default-panel ]; then printf 'secondary-panel-node:\n' >> /sdcard/adtbloader/identity.txt; find /sys/firmware/devicetree/base/soc/qcom,mdss_mdp@* -maxdepth 2 -type f -name phandle -exec cmp -s /sys/firmware/devicetree/base/soc/qcom,dsi-display-secondary/qcom,dsi-default-panel {} \; -exec dirname {} \; >> /sdcard/adtbloader/identity.txt; fi printf 'fdt: /sdcard/adtbloader/android.dtb\n' >> /sdcard/adtbloader/identity.txt -chmod 0644 /sdcard/adtbloader/identity.txt /sdcard/adtbloader/android.dtb +printf 'slot-suffix: %s\n' "$(getprop ro.boot.slot_suffix)" >> /sdcard/adtbloader/identity.txt +dd if="/dev/block/by-name/dtbo$(getprop ro.boot.slot_suffix)" of=/sdcard/adtbloader/active-dtbo.img bs=1048576 +chmod 0644 /sdcard/adtbloader/identity.txt /sdcard/adtbloader/android.dtb /sdcard/adtbloader/active-dtbo.img diff --git a/src/device.c b/src/device.c index afd8e11..d208c0f 100644 --- a/src/device.c +++ b/src/device.c @@ -21,6 +21,27 @@ struct armada_device_match { const char *secondary_panel; }; +#define ANDROID_DT_TABLE_MAGIC 0xd7b7ab1e + +struct android_dt_table_header { + fdt32_t magic; + fdt32_t total_size; + fdt32_t header_size; + fdt32_t entry_size; + fdt32_t entry_count; + fdt32_t entries_offset; + fdt32_t page_size; + fdt32_t version; +}; + +struct android_dt_table_entry { + fdt32_t dt_size; + fdt32_t dt_offset; + fdt32_t id; + fdt32_t rev; + fdt32_t custom[4]; +}; + static struct armada_device_match armada_devices[] = { { .device = { @@ -49,13 +70,20 @@ static struct armada_device_match armada_devices[] = { }, }; -static bool panel_name_is(void *dtb, const char *display_path, const char *expected) +static bool panel_name_is(void *dtb, const char *label, const char *expected) { const fdt32_t *panel_phandle; - const char *panel_name; + const char *display_label, *panel_name; int display, panel, len; - display = fdt_path_offset(dtb, display_path); + display = -1; + while ((display = fdt_node_offset_by_compatible( + dtb, display, "qcom,dsi-display")) >= 0) { + display_label = fdt_getprop(dtb, display, "label", &len); + if (display_label && fdt_stringlist_contains(display_label, len, label)) + break; + } + if (display < 0) return false; @@ -71,31 +99,23 @@ static bool panel_name_is(void *dtb, const char *display_path, const char *expec return panel_name && fdt_stringlist_contains(panel_name, len, expected); } -static struct device *match_armada_device(void) +static struct device *match_armada_dtb(void *android_dtb) { - EFI_GUID dtb_table_guid = EFI_DTB_TABLE_GUID; struct device *match = NULL; - void *android_dtb; unsigned i; - if (EFI_ERROR(LibGetSystemConfigurationTable(&dtb_table_guid, &android_dtb))) - return NULL; - - if (fdt_check_header(android_dtb)) - return NULL; - for (i = 0; i < ARRAY_SIZE(armada_devices); ++i) { struct armada_device_match *dev = &armada_devices[i]; if (fdt_node_check_compatible(android_dtb, 0, dev->android_compatible)) continue; - if (!panel_name_is(android_dtb, "/soc/qcom,dsi-display-primary", + if (!panel_name_is(android_dtb, "primary", dev->primary_panel)) continue; if (dev->secondary_panel && - !panel_name_is(android_dtb, "/soc/qcom,dsi-display-secondary", + !panel_name_is(android_dtb, "secondary", dev->secondary_panel)) continue; @@ -108,6 +128,79 @@ static struct device *match_armada_device(void) return match; } +static struct device *match_armada_dtbo(void *dtbo, UINTN len) +{ + struct android_dt_table_header *header = dtbo; + struct device *match = NULL; + UINT32 entries_offset, entry_count, entry_size; + unsigned i; + + if (len < sizeof(*header) || + fdt32_to_cpu(header->magic) != ANDROID_DT_TABLE_MAGIC || + fdt32_to_cpu(header->total_size) > len || + fdt32_to_cpu(header->header_size) < sizeof(*header)) + return NULL; + + entries_offset = fdt32_to_cpu(header->entries_offset); + entry_count = fdt32_to_cpu(header->entry_count); + entry_size = fdt32_to_cpu(header->entry_size); + if (entry_size < sizeof(struct android_dt_table_entry) || + entries_offset > len || + entry_count > (len - entries_offset) / entry_size) + return NULL; + + for (i = 0; i < entry_count; ++i) { + struct android_dt_table_entry *entry; + struct device *candidate; + UINT32 dt_offset, dt_size; + void *dtb; + + entry = (void *)((UINT8 *)dtbo + entries_offset + i * entry_size); + dt_offset = fdt32_to_cpu(entry->dt_offset); + dt_size = fdt32_to_cpu(entry->dt_size); + if (dt_offset > len || dt_size > len - dt_offset) + continue; + + dtb = (UINT8 *)dtbo + dt_offset; + if (fdt_check_header(dtb) || fdt_totalsize(dtb) > dt_size) + continue; + + candidate = match_armada_dtb(dtb); + if (!candidate) + continue; + if (match && match != candidate) + return NULL; + + match = candidate; + } + + return match; +} + +static struct device *match_armada_device(void) +{ + EFI_GUID dtb_table_guid = EFI_DTB_TABLE_GUID; + struct device *match = NULL; + void *android_dtb, *dtbo; + EFI_STATUS status; + UINTN dtbo_len; + + status = LibGetSystemConfigurationTable(&dtb_table_guid, &android_dtb); + if (!EFI_ERROR(status) && !fdt_check_header(android_dtb)) { + match = match_armada_dtb(android_dtb); + if (match) + return match; + } + + status = qcom_read_active_dtbo(&dtbo, &dtbo_len); + if (EFI_ERROR(status)) + return NULL; + + match = match_armada_dtbo(dtbo, dtbo_len); + FreePool(dtbo); + return match; +} + /** * match_device() - Detect the device. diff --git a/src/include/device.h b/src/include/device.h index 21ddc03..755d933 100644 --- a/src/include/device.h +++ b/src/include/device.h @@ -49,5 +49,6 @@ EFI_STATUS dt_update_mac(void *dtb, const char * const compatibles[], unsigned n /* qcom.c */ EFI_STATUS qcom_dt_set_dpp_mac(struct device *dev, void *dtb); +EFI_STATUS qcom_read_active_dtbo(void **dtbo, UINTN *len); #endif diff --git a/src/qcom.c b/src/qcom.c index a558318..d93e28d 100644 --- a/src/qcom.c +++ b/src/qcom.c @@ -25,6 +25,8 @@ typedef struct { #define PARTITION_TYPE_OTHER 0x00 #define PARTITION_TYPE_MBR 0x01 #define PARTITION_TYPE_GPT 0x02 +#define QCOM_AB_PARTITION_ACTIVE (1ULL << 50) +#define ANDROID_DT_TABLE_MAGIC 0xd7b7ab1e typedef struct { @@ -73,6 +75,111 @@ static EFI_STATUS locate_gpt_partition(CHAR16 *name, EFI_HANDLE *partition_handl return EFI_NOT_FOUND; } +static EFI_STATUS partition_attributes(EFI_HANDLE partition_handle, + UINT64 *attributes) +{ + EFI_GUID partition_info_guid = EFI_PARTITION_INFO_PROTOCOL_GUID; + EFI_PARTITION_INFO_PROTOCOL *partition; + EFI_STATUS status; + + status = uefi_call_wrapper(BS->HandleProtocol, 3, partition_handle, + &partition_info_guid, (void **)&partition); + if (EFI_ERROR(status)) + return status; + + if (partition->Type != PARTITION_TYPE_GPT) + return EFI_UNSUPPORTED; + + *attributes = partition->Info.Gpt.Attributes; + return EFI_SUCCESS; +} + +static EFI_STATUS locate_active_slot_partition(EFI_HANDLE *partition_handle) +{ + static CHAR16 *boot_names[] = { L"boot_a", L"boot_b" }; + static CHAR16 *dtbo_names[] = { L"dtbo_a", L"dtbo_b" }; + EFI_HANDLE boot[2]; + EFI_STATUS status; + UINT64 attributes[2]; + unsigned active, i; + + for (i = 0; i < 2; ++i) { + status = locate_gpt_partition(boot_names[i], &boot[i]); + if (EFI_ERROR(status)) + return status; + + status = partition_attributes(boot[i], &attributes[i]); + if (EFI_ERROR(status)) + return status; + } + + if (!!(attributes[0] & QCOM_AB_PARTITION_ACTIVE) == + !!(attributes[1] & QCOM_AB_PARTITION_ACTIVE)) + return EFI_NOT_FOUND; + + active = attributes[1] & QCOM_AB_PARTITION_ACTIVE ? 1 : 0; + return locate_gpt_partition(dtbo_names[active], partition_handle); +} + +EFI_STATUS qcom_read_active_dtbo(void **dtbo, UINTN *len) +{ + struct { + fdt32_t magic; + fdt32_t total_size; + } header; + EFI_BLOCK_IO_PROTOCOL *block_io; + EFI_DISK_IO_PROTOCOL *disk_io; + EFI_HANDLE partition; + EFI_STATUS status; + UINT64 partition_size; + UINTN size; + void *buf; + + status = locate_active_slot_partition(&partition); + if (EFI_ERROR(status)) + return status; + + status = uefi_call_wrapper(BS->HandleProtocol, 3, partition, + &gEfiBlockIoProtocolGuid, (void **)&block_io); + if (EFI_ERROR(status)) + return status; + + status = uefi_call_wrapper(BS->HandleProtocol, 3, partition, + &gEfiDiskIoProtocolGuid, (void **)&disk_io); + if (EFI_ERROR(status)) + return status; + + status = uefi_call_wrapper(disk_io->ReadDisk, 5, disk_io, + block_io->Media->MediaId, 0, + sizeof(header), &header); + if (EFI_ERROR(status)) + return status; + + if (fdt32_to_cpu(header.magic) != ANDROID_DT_TABLE_MAGIC) + return EFI_UNSUPPORTED; + + size = fdt32_to_cpu(header.total_size); + partition_size = (block_io->Media->LastBlock + 1) * + block_io->Media->BlockSize; + if (size < sizeof(header) || size > partition_size) + return EFI_BAD_BUFFER_SIZE; + + buf = AllocatePool(size); + if (!buf) + return EFI_OUT_OF_RESOURCES; + + status = uefi_call_wrapper(disk_io->ReadDisk, 5, disk_io, + block_io->Media->MediaId, 0, size, buf); + if (EFI_ERROR(status)) { + FreePool(buf); + return status; + } + + *dtbo = buf; + *len = size; + return EFI_SUCCESS; +} + /** * locate_partition_by_magic() - Find partition handle with first bytes matching magic. */