Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ attempts to simplify running Linux-based or other OS that use DT by providing:
- <!-- x1p42100 --> ASUS Zenbook A14 UX3407QA
- <!-- x1e80100 --> ASUS Zenbook A14 UX3407RA
- *<!-- glymur --> Asus Zenbook A16 UX3607OA*
- <!-- cq8725s --> AYN Odin 3
- <!-- qcs8550 --> AYN Thor
- <!-- x1e80100 --> Dell Inspiron 14 Plus 7441
- *<!-- x1p64100 --> Dell Latitude 5455*
Expand Down
4 changes: 3 additions & 1 deletion scripts/describe_android_dt.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
178 changes: 153 additions & 25 deletions src/device.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,76 @@
__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",
#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;
};

static bool panel_name_is(void *dtb, const char *display_path, const char *expected)
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 = {
.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 *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;

Expand All @@ -46,31 +99,106 @@ 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;
void *android_dtb;
struct device *match = NULL;
unsigned i;

if (EFI_ERROR(LibGetSystemConfigurationTable(&dtb_table_guid, &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, "primary",
dev->primary_panel))
continue;

if (dev->secondary_panel &&
!panel_name_is(android_dtb, "secondary",
dev->secondary_panel))
continue;

if (match)
return NULL;

match = &dev->device;
}

return match;
}

if (fdt_check_header(android_dtb))
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;

if (fdt_node_check_compatible(android_dtb, 0, "qcom,kalamap-hdk"))
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;

if (panel_name_is(android_dtb, "/soc/qcom,dsi-display-primary",
"il97680a amoled panel without DSC"))
return &armada_retroid_pocket_nova;
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;
}

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;
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;

return NULL;
match = match_armada_dtbo(dtbo, dtbo_len);
FreePool(dtbo);
return match;
}


Expand Down
1 change: 1 addition & 0 deletions src/include/device.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
107 changes: 107 additions & 0 deletions src/qcom.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -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.
*/
Expand Down
Loading