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
129 changes: 101 additions & 28 deletions src/chainload.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ _Static_assert(offsetof(struct chainload_header, run_base) == CHAINLOAD_HDR_RUN_
* Return how many bytes from its base an image needs to run at a run_base below its staged copy
* (chainload.h), or 0 if it does not support that.
*/
static u64 chainload_run_footprint(const void *image, size_t size)
static u64 chainload_run_footprint(const void *image, size_t size, u32 *flags)
{
struct chainload_header hdr;

Expand All @@ -48,26 +48,60 @@ static u64 chainload_run_footprint(const void *image, size_t size)
!(hdr.flags & CHAINLOAD_HDR_F_RUN_BASE) || hdr.run_base || !hdr.footprint ||
(hdr.footprint & (SZ_16K - 1)) || hdr.footprint > size)
return 0;
if (flags)
*flags = hdr.flags;
return hdr.footprint;
}

#define LIVE_DCP_ADT_ENTRY_SIZE (sizeof(struct adt_property) + sizeof(u64))

/* A copied tree has one new property before its memory-map child. */
static int chainload_set_adt_range(void *copy, int node, const char *name, u64 range[2])
{
if (!copy)
return adt_setprop(adt, node, name, range, 2 * sizeof(u64));

u32 size;
const u8 *value = adt_getprop(adt, node, name, &size);
if (!value || size != 2 * sizeof(u64))
return -1;
size_t offset = value - (const u8 *)adt;
if (offset > cur_boot_args.devtree_size || size > cur_boot_args.devtree_size - offset)
return -1;
memcpy((u8 *)copy + offset + LIVE_DCP_ADT_ENTRY_SIZE, range, size);
return 0;
}

int chainload_image(void *image, size_t size, char **vars, size_t var_cnt)
{
u64 new_base = (u64)_base;
u64 image_size = size;
u64 ram_end, ro_start = 0, ro_end = 0;
u64 live_heap = heapblock_high_water();
u64 live_heap;
u64 inherited_top = cur_boot_args.top_of_kernel_data;
u64 protected_heap_start = inherited_top;
int ro_status = memory_fw_ro_range(&ro_start, &ro_end);

printf("chainload: Preparing image...\n");
next_stage.entry = NULL;
if (!image || !size || ro_status < 0 ||
cur_boot_args.phys_base > UINT64_MAX - cur_boot_args.mem_size)
return -1;
u32 target_flags = 0;
u64 target_footprint = chainload_run_footprint(image, size, &target_flags);
if (!(target_flags & CHAINLOAD_HDR_F_LIVE_DCP) && display_needs_live_receiver()) {
printf("chainload: next image cannot inherit the running DCP\n");
return -1;
}
/* Capture any newly retained DCP buffers before laying out the next image. */
if (display_prepare_live_handoff() < 0)
return -1;
live_heap = heapblock_high_water();
ram_end = cur_boot_args.phys_base + cur_boot_args.mem_size;
display_inherited_live_heap(&protected_heap_start);
if (new_base < cur_boot_args.phys_base || new_base >= ram_end || live_heap > ram_end ||
inherited_top > ram_end)
inherited_top > ram_end || protected_heap_start > inherited_top ||
protected_heap_start < cur_boot_args.phys_base)
return -1;

for (size_t i = 0; i < var_cnt; i++) {
Expand Down Expand Up @@ -108,6 +142,25 @@ int chainload_image(void *image, size_t size, char **vars, size_t var_cnt)
if (fw_bootargs_off > UINT64_MAX - SZ_16K)
return -1;
u64 fw_size = fw_bootargs_off + SZ_16K;
int chosen = -1, prop_offset = -1;
size_t adt_size = cur_boot_args.devtree_size;
u64 adt_off = fw_size;
if (display_handed_on_live()) {
/* Build a private ADT copy for the next stage. The current stage may retry. */
chosen = adt_path_offset(adt, "/chosen");
if (chosen < 0)
return -1;
if (adt_get_property_count(adt, chosen) <= 0)
return -1;
prop_offset = adt_first_property_offset(adt, chosen);
if (prop_offset < 0 || (size_t)prop_offset > adt_size ||
adt_size > UINT32_MAX - LIVE_DCP_ADT_ENTRY_SIZE)
return -1;
adt_size += LIVE_DCP_ADT_ENTRY_SIZE;
if (fw_size > UINT64_MAX - ALIGN_UP(adt_size, SZ_16K))
return -1;
fw_size += ALIGN_UP(adt_size, SZ_16K);
}
if (image_size > UINT64_MAX - fw_size || new_base > UINT64_MAX - image_size - fw_size)
return -1;

Expand All @@ -123,7 +176,7 @@ int chainload_image(void *image, size_t size, char **vars, size_t var_cnt)
* base (start.S) while its payload, SEPFW and boot_args stay in the staged copy, below
* the new high-water.
*/
footprint = chainload_run_footprint(image, size);
footprint = target_footprint;
if (!footprint || (new_base & (SZ_16K - 1)) || new_base >= ro_start ||
footprint > ro_start - new_base) {
printf("chainload: image overlaps firmware RO and cannot run below it\n");
Expand All @@ -133,27 +186,28 @@ int chainload_image(void *image, size_t size, char **vars, size_t var_cnt)
} else if (packed_end > ram_end) {
printf("chainload: image destination is invalid or overlaps firmware RO\n");
return -1;
} else if (live_heap > inherited_top && image_end > inherited_top && new_base < live_heap) {
} else if (live_heap > protected_heap_start && image_end > protected_heap_start &&
new_base < live_heap) {
/*
* The next stage is larger than the room our caller left between our base and the heap
* (a Stage 1 smaller than its Stage 2 plus the firmware block), so a copy to our base
* would run over the heap that holds the staged image. An aura image can run from where
* it is staged instead and move only its first footprint bytes to our base, which is
* safe while those stay below the heap.
*/
footprint = chainload_run_footprint(image, size);
if (!footprint || (new_base & (SZ_16K - 1)) || inherited_top <= new_base ||
footprint > inherited_top - new_base) {
footprint = target_footprint;
if (!footprint || (new_base & (SZ_16K - 1)) || protected_heap_start <= new_base ||
footprint > protected_heap_start - new_base) {
printf("chainload: image span overlaps the live heap\n");
return -1;
}
must_stage = true;
}

bool park_fw =
!run_base && !must_stage &&
((ro_status == 1 && packed_end > ro_start && image_end < ro_end) ||
(live_heap > inherited_top && packed_end > inherited_top && image_end < live_heap));
bool park_fw = !run_base && !must_stage &&
((ro_status == 1 && packed_end > ro_start && image_end < ro_end) ||
(live_heap > protected_heap_start && packed_end > protected_heap_start &&
image_end < live_heap));
u8 *parked = NULL;
u64 fw_base = image_end;
if (park_fw) {
Expand Down Expand Up @@ -189,7 +243,7 @@ int chainload_image(void *image, size_t size, char **vars, size_t var_cnt)
copy_size + stub_size <= ram_end - staged &&
!(ro_status == 1 && staged < ro_end && staged + copy_size + stub_size > ro_start);
if (!run_base && !park_fw && fits_staged) {
u64 run_footprint = chainload_run_footprint(image, size);
u64 run_footprint = target_footprint;
if (run_footprint && !(new_base & (SZ_16K - 1)) && staged >= new_base + run_footprint &&
(ro_status != 1 || new_base + run_footprint <= ro_start || new_base >= ro_end)) {
footprint = run_footprint;
Expand Down Expand Up @@ -243,28 +297,42 @@ int chainload_image(void *image, size_t size, char **vars, size_t var_cnt)
high_water = max(high_water, ro_end);
new_boot_args->top_of_kernel_data = high_water;

u64 new_sepfw[2] = {fw_base, sepfw[1]};
if (adt_setprop(adt, anode, "SEPFW", new_sepfw, sizeof(new_sepfw)) < 0) {
free(new_image);
free(parked);
return -1;
u8 *next_adt = NULL;
if (chosen >= 0) {
next_adt = fw_data + adt_off;
struct adt_property *prop = (void *)(next_adt + prop_offset);
struct adt_node_hdr *node = (void *)(next_adt + chosen);
u64 next_adt_pa = fw_base + adt_off;

memcpy(next_adt, adt, prop_offset);
memcpy(prop->name, DISPLAY_LIVE_ADT_PROP, sizeof(DISPLAY_LIVE_ADT_PROP));
prop->size = sizeof(inherited_top);
memcpy(prop->value, &inherited_top, sizeof(inherited_top));
memcpy(prop->value + sizeof(inherited_top), (u8 *)adt + prop_offset,
cur_boot_args.devtree_size - prop_offset);
node->property_count++;
new_boot_args->devtree =
(void *)(next_adt_pa - cur_boot_args.phys_base + cur_boot_args.virt_base);
new_boot_args->devtree_size = adt_size;
u64 tree_range[2] = {next_adt_pa, ALIGN_UP(adt_size, SZ_16K)};
if (adt_getprop(adt, anode, "DeviceTree", NULL) &&
chainload_set_adt_range(next_adt, anode, "DeviceTree", tree_range) < 0)
goto bad_adt_range;
}

u64 new_sepfw[2] = {fw_base, sepfw[1]};
if (chainload_set_adt_range(next_adt, anode, "SEPFW", new_sepfw) < 0)
goto bad_adt_range;
if (preoslog[1]) {
preoslog[0] = fw_base + fw_preoslog_off;
if (adt_setprop(adt, anode, "preoslog", preoslog, sizeof(preoslog)) < 0) {
free(new_image);
free(parked);
return -1;
}
if (chainload_set_adt_range(next_adt, anode, "preoslog", preoslog) < 0)
goto bad_adt_range;
}
/* Point the memory map at the moved boot arguments, where the ADT lists them. */
u64 bootargs_map[2] = {fw_base + fw_bootargs_off, SZ_16K};
if (adt_getprop(adt, anode, "BootArgs", NULL) &&
adt_setprop(adt, anode, "BootArgs", bootargs_map, sizeof(bootargs_map)) < 0) {
free(new_image);
free(parked);
return -1;
}
chainload_set_adt_range(next_adt, anode, "BootArgs", bootargs_map) < 0)
goto bad_adt_range;
if (parked) {
dc_cvac_range(parked, fw_size);
sysop("dsb sy");
Expand All @@ -291,6 +359,11 @@ int chainload_image(void *image, size_t size, char **vars, size_t var_cnt)
next_stage.args[4] = new_base + CHAINLOAD_ENTRY_OFFSET;
next_stage.restore_logo = false;
return 0;

bad_adt_range:
free(new_image);
free(parked);
return -1;
}

#ifdef CHAINLOADING
Expand Down
1 change: 1 addition & 0 deletions src/chainload.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#define CHAINLOAD_HDR_MAGIC 0x44414f4c41525541 /* "AURALOAD" */
#define CHAINLOAD_HDR_VERSION 1
#define CHAINLOAD_HDR_F_RUN_BASE 1
#define CHAINLOAD_HDR_F_LIVE_DCP 2 /* consumes the live DCP marker in the ADT */
#define CHAINLOAD_HDR_FOOTPRINT_OFF 0x10
#define CHAINLOAD_HDR_RUN_BASE_OFF 0x18
#define CHAINLOAD_HDR_SIZE 0x20
Expand Down
7 changes: 5 additions & 2 deletions src/dart.c
Original file line number Diff line number Diff line change
Expand Up @@ -683,8 +683,11 @@ dart_dev_t *dart_init_adt_display(const char *path, int instance, int device)
u32 tcr = read32(DART_TCR(dart));
bool enabled, translating;
if (dart->type == DART_T8110) {
u32 mode = DART_T8110_TCR_TRANSLATE_ENABLE | DART_T8110_TCR_BYPASS_DART |
DART_T8110_TCR_BYPASS_DAPF | DART_T8110_TCR_REMAP_EN;
/* Bypassing only the DAPF filter stage leaves translation through the tables on, as the
* rest of this file reads TCR: iBoot locks dart-dispext0 that way (TCR 0x5) on firmware 27.
*/
u32 mode =
DART_T8110_TCR_TRANSLATE_ENABLE | DART_T8110_TCR_BYPASS_DART | DART_T8110_TCR_REMAP_EN;
enabled = read32(dart->regs + DART_T8110_ENABLE_STREAMS + 4 * (dart->device >> 5)) &
BIT(dart->device & 31);
translating = (tcr & mode) == DART_T8110_TCR_TRANSLATE_ENABLE;
Expand Down
51 changes: 43 additions & 8 deletions src/dcp.c
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ static int dcp_hdmi_dptx_init(dcp_dev_t *dcp, const display_config_t *cfg)
printf("dcp: failed to find dp2hdmi-gpio node '%s'\n", cfg->dp2hdmi_gpio);
return -1;
}
dcp->hpd_on_ap = adt_getprop(adt, node, "function-hdmi_hpd", NULL) != NULL;
struct adt_function_smc_gpio dp2hdmi_pwr, hdmi_pwr;
u32 pw8_size;
const struct adt_function_smc_pw8 *pw8_dp2hdmi =
Expand Down Expand Up @@ -166,6 +167,18 @@ static int dcp_hdmi_dptx_init(dcp_dev_t *dcp, const display_config_t *cfg)
return -1;
}

/*
* The firmware announces which message format its dptx-port service speaks. The AFKv2
* format is the same on every firmware that has it. The older format's layouts were only
* checked against the 13.5 firmware, so other firmware with that format is not driven.
*/
if (!dcp_dptx_is_afkv2(dcp->dptx_ep) && os_firmware.version != V13_5) {
printf("dcp: dptx-port uses the pre-AFKv2 format, checked on 13.5 firmware only\n");
dcp_dptx_shutdown(dcp->dptx_ep);
dcp_dpav_shutdown(dcp->dpav_ep);
return -1;
}

#ifdef RTKIT_SYSLOG
// start system endpoint when extended logging is requested
dcp->system_ep = dcp_system_init(dcp);
Expand All @@ -182,6 +195,16 @@ static int dcp_hdmi_dptx_init(dcp_dev_t *dcp, const display_config_t *cfg)
return 0;
}

/*
* Whether the firmware has to be told about HPD. When the converter's HPD reaches an AP gpio
* (function-hdmi_hpd on its dp2hdmi-gpio node) instead of the phy, a dptx-port service that speaks
* AFKv2 brings the port up only once the AP reports HPD. The older format never needed it.
*/
bool dcp_dptx_needs_ap_hpd(dcp_dev_t *dcp)
{
return dcp->hpd_on_ap && dcp_dptx_is_afkv2(dcp->dptx_ep);
}

int dcp_connect_dptx(dcp_dev_t *dcp)
{
if (dcp->dptx_ep && dcp->phy) {
Expand Down Expand Up @@ -271,14 +294,6 @@ dcp_dev_t *dcp_init(const display_config_t *cfg)
dcp_init_quiesced = false;

if (cfg && cfg->dptx_phy[0]) {
/* J873g reports mBoot-20457.1.29, which is not in the OS firmware
* table, and the same dptx-port endpoint works on its USB-C route.
* Keep the V13_5 restriction for the machines it was written for. */
if (os_firmware.version != V13_5 && chip_id != T8152) {
printf("dcp: dtpx-port is only supported with V13_5 OS firmware.\n");
return NULL;
}

strncpy(dcp_pmgr_dev, cfg->pmgr_dev, sizeof(dcp_pmgr_dev));
dcp_die = cfg->die;
pmgr_adt_power_enable(cfg->dcp);
Expand Down Expand Up @@ -435,6 +450,26 @@ int dcp_handoff(dcp_dev_t *dcp)
return ret;
}

static bool dcp_handed_on;

/*
* Hand a running DCP on to the next stage: its endpoints stopped (dcp_handoff()), RTKit, its
* buffers and DART mappings and the scanout left running. It counts as running from here on, so
* it is not started again and the next stage keeps its memory (kboot reserves the heap).
*/
int dcp_hand_on(dcp_dev_t *dcp)
{
int ret = dcp_handoff(dcp);
dcp_running = true;
dcp_handed_on = !ret;
return ret;
}

bool dcp_handed_on_live(void)
{
return dcp_handed_on;
}

int dcp_shutdown(dcp_dev_t *dcp, bool sleep)
{
/* The ASCWrap-v8 sleep path needs a PMGR2 reset, which is not implemented
Expand Down
4 changes: 4 additions & 0 deletions src/dcp.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,19 @@ typedef struct dcp_dev {
u32 die;
u32 dp2hdmi_pwr_gpio;
u32 hdmi_pwr_gpio;
bool hpd_on_ap; /* the converter's HPD reaches an AP gpio (ADT function-hdmi_hpd) */
} dcp_dev_t;

int dcp_connect_dptx(dcp_dev_t *dcp);
bool dcp_dptx_needs_ap_hpd(dcp_dev_t *dcp);
int dcp_work(dcp_dev_t *dcp);

dcp_dev_t *dcp_init(const display_config_t *config);

int dcp_shutdown(dcp_dev_t *dcp, bool sleep);
int dcp_handoff(dcp_dev_t *dcp);
int dcp_hand_on(dcp_dev_t *dcp);
bool dcp_handed_on_live(void);
bool dcp_left_running(void);
u32 t8152_psd_state(u32 service);
bool dcp_init_left_quiesced(void);
Expand Down
Loading
Loading