Skip to content

Apple's open-source XNU kernel on Android phones: its own loader and drivers, Metal on the Adreno. Runs from RAM; a reboot gives the phone back to Android.

C

battery: full UEFI charger stack + real percent in the status bar

shimoku7463c7aecd65fd
diff --git a/userland/src/inject_sb.c b/userland/src/inject_sb.c
index 754f91c..e7020ab 100644
--- a/userland/src/inject_sb.c
+++ b/userland/src/inject_sb.c
@@ -145,20 +145,26 @@ snapshot_output_format(__unused id self, __unused SEL selector, id snapshot)
return SNAPSHOT_FORMAT_PNG;
}
-/* ---- real battery level from the PM7250B fuel gauge ---------------------------------------------- */
+/* ---- battery state from redwood's Qualcomm charger stack ----------------------------------------- */
/*
* iOS reads battery level through UIDevice.batteryLevel and UIDevice.batteryState; SpringBoard's
* status bar reads it the same way via SBBatteryController. There is no IOPMPowerSource kernel
- * driver yet (that is the right long-term fix; see redwood_battery.c for the register work), so
- * we hook the UIDevice accessors to return what the kernel's SPMI poller found, which it stores
- * in kern.redwood.battery_soc (0..100) and kern.redwood.battery_current_ma (negative = charging).
+ * driver yet (that is the right long-term fix), so we hook the UIDevice accessors to return what
+ * xnuloader reads through Qualcomm's UEFI charger protocols and XNU publishes as
+ * kern.redwood.battery_soc (0..100) and kern.redwood.battery_charging.
* When the sysctl is missing (battery=1 not in boot-args, or the driver never started) we hand
* back the value the real implementation computed; the status bar then shows whatever iOS last
* knew, which is better than crashing.
*/
-static float (*original_battery_level)(id self, SEL sel);
-static long (*original_battery_state)(id self, SEL sel);
+static float (*original_battery_level)(id self, SEL sel);
+static long (*original_battery_state)(id self, SEL sel);
+static BOOL (*original_battery_view_shows_percentage)(id self, SEL sel);
+static void (*original_battery_view_set_shows_percentage)(id self, SEL sel, BOOL shows);
+static BOOL (*original_battery_view_should_show_bolt)(id self, SEL sel);
+static double (*original_battery_view_charge_percent)(id self, SEL sel);
+static void (*original_battery_view_set_charge_percent)(id self, SEL sel, double p);
+static double (*original_ring_charging_level)(id self, SEL sel);
static int
sysctl_int(const char *name)
@@ -186,16 +192,102 @@ battery_level(id self, SEL sel)
static long
battery_state(id self, SEL sel)
{
- int ma = sysctl_int("kern.redwood.battery_current_ma");
+ int charging = sysctl_int("kern.redwood.battery_charging");
+ int present = sysctl_int("kern.redwood.battery_present");
+ int plugged = sysctl_int("kern.redwood.battery_charger_present");
int soc = sysctl_int("kern.redwood.battery_soc");
- if (ma == -1 && soc == -1) {
+ if (charging == -1 || present == -1 || plugged == -1 || soc < 0 || soc > 100) {
return original_battery_state ? original_battery_state(self, sel) : 0;
}
- if (ma < -50) { /* negative current = charging */
- return soc >= 99 ? 3 : 2;
+ if (!present) return 0;
+ if (plugged && soc >= 100) return 3;
+ return charging ? 2 : 1;
+}
+
+/* UIKitCore's status-bar view has its own presentation policy, separate from UIDevice. */
+static BOOL
+battery_view_shows_percentage(id self, SEL sel)
+{
+ int soc = sysctl_int("kern.redwood.battery_soc");
+
+ if (soc >= 0 && soc <= 100) return YES;
+ return original_battery_view_shows_percentage ? original_battery_view_shows_percentage(self, sel) : NO;
+}
+
+static void
+battery_view_set_shows_percentage(id self, SEL sel, BOOL shows)
+{
+ int soc = sysctl_int("kern.redwood.battery_soc");
+
+ original_battery_view_set_shows_percentage(self, sel, (soc >= 0 && soc <= 100) ? YES : shows);
+}
+
+static BOOL
+battery_view_should_show_bolt(id self, SEL sel)
+{
+ int charging = sysctl_int("kern.redwood.battery_charging");
+ int present = sysctl_int("kern.redwood.battery_present");
+ int plugged = sysctl_int("kern.redwood.battery_charger_present");
+
+ if (charging != -1 && present != -1 && plugged != -1) return charging && present && plugged;
+ return original_battery_view_should_show_bolt ? original_battery_view_should_show_bolt(self, sel) : NO;
+}
+
+/*
+ * The actual number the status bar draws. _UIBatteryView keeps the fill fraction (0.0..1.0) in
+ * chargePercent; it is set through setChargePercent: and read back for layout. iOS feeds it from
+ * BatteryCenter / IOPowerSources, which is empty on this phone, so without us it sits near zero
+ * (the "1%" the user saw). We override both sides: setChargePercent: so the stored value is ours
+ * no matter who writes it, and the getter so any layout path that reads it agrees.
+ */
+static double
+battery_view_charge_percent(id self, SEL sel)
+{
+ int soc = sysctl_int("kern.redwood.battery_soc");
+
+ if (soc >= 0 && soc <= 100) return (double)soc / 100.0;
+ return original_battery_view_charge_percent ? original_battery_view_charge_percent(self, sel) : 0.0;
+}
+
+static void
+battery_view_set_charge_percent(id self, SEL sel, double p)
+{
+ int soc = sysctl_int("kern.redwood.battery_soc");
+
+ original_battery_view_set_charge_percent(self, sel, (soc >= 0 && soc <= 100) ? (double)soc / 100.0 : p);
+}
+
+/* The big charging ring on the lock screen (CoverSheet) reads its fill level the same way. */
+static double
+ring_charging_level(id self, SEL sel)
+{
+ int soc = sysctl_int("kern.redwood.battery_soc");
+
+ if (soc >= 0 && soc <= 100) return (double)soc / 100.0;
+ return original_ring_charging_level ? original_ring_charging_level(self, sel) : 0.0;
+}
+
+/* Keep SpringBoard's normal battery observers awake while the XNU cache changes. */
+static void
+battery_post_notifications(void)
+{
+ Class uidev = objc_getClass("UIDevice");
+ Class notification_center = objc_getClass("NSNotificationCenter");
+ id device = uidev ? ((id (*)(Class, SEL))objc_msgSend)(uidev, sel_registerName("currentDevice")) : nil;
+ id center = notification_center ? ((id (*)(Class, SEL))objc_msgSend)(notification_center,
+ sel_registerName("defaultCenter")) : nil;
+
+ if (center != nil) {
+ SEL post = sel_registerName("postNotificationName:object:");
+ ((void (*)(id, SEL, id, id))objc_msgSend)(center, post,
+ (id)CFSTR("UIDeviceBatteryLevelDidChangeNotification"), device);
+ ((void (*)(id, SEL, id, id))objc_msgSend)(center, post,
+ (id)CFSTR("UIDeviceBatteryStateDidChangeNotification"), device);
}
- return 1;
+ dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 5 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
+ battery_post_notifications();
+ });
}
/* ---- the lock screen's clock without a GPU ------------------------------------------------------- */
@@ -509,8 +601,20 @@ redwood_springboard_init(void)
* hooking them here in the SpringBoard process covers both the status bar and any app that
* reads [UIDevice currentDevice].batteryLevel. The addresses are for iOS 16.5 (20F66).
*/
- original_battery_level = (float (*)(id, SEL))replace_method("UIDevice", "batteryLevel", 0x1aa038b5cUL, (IMP)battery_level);
- original_battery_state = (long (*)(id, SEL))replace_method("UIDevice", "batteryState", 0x1aa038b90UL, (IMP)battery_state);
+ original_battery_level = (float (*)(id, SEL))replace_method("UIDevice", "batteryLevel", 0x1897021b0UL, (IMP)battery_level);
+ original_battery_state = (long (*)(id, SEL))replace_method("UIDevice", "batteryState", 0x189209794UL, (IMP)battery_state);
+ original_battery_view_shows_percentage = (BOOL (*)(id, SEL))replace_method("_UIBatteryView", "showsPercentage",
+ 0x189a9370cUL, (IMP)battery_view_shows_percentage);
+ original_battery_view_set_shows_percentage = (void (*)(id, SEL, BOOL))replace_method("_UIBatteryView", "setShowsPercentage:",
+ 0x18926e150UL, (IMP)battery_view_set_shows_percentage);
+ original_battery_view_should_show_bolt = (BOOL (*)(id, SEL))replace_method("_UIBatteryView", "_shouldShowBolt",
+ 0x189203e28UL, (IMP)battery_view_should_show_bolt);
+ original_battery_view_charge_percent = (double (*)(id, SEL))replace_method("_UIBatteryView", "chargePercent",
+ 0x189a935d8UL, (IMP)battery_view_charge_percent);
+ original_battery_view_set_charge_percent = (void (*)(id, SEL, double))replace_method("_UIBatteryView", "setChargePercent:",
+ 0x18928d310UL, (IMP)battery_view_set_charge_percent);
+ original_ring_charging_level = (double (*)(id, SEL))replace_method("CSBatteryChargingRingView", "batteryChargingLevel",
+ 0x1c48c2cf4UL, (IMP)ring_charging_level);
battery_lvl = (IMP)original_battery_level;
battery_st = (IMP)original_battery_state;
say("redwood: SpringBoard: activation %s, setup assistant %s, application snapshots %s, the side button's long press %s, "
@@ -518,6 +622,13 @@ redwood_springboard_init(void)
activation ? "answered here" : "NOT replaced", original_set_setup_required_reason ? "refused" : "NOT replaced",
snapshot_format ? "as JPEG" : "NOT changed", original_lock_long_press ? "checked" : "NOT checked",
battery_lvl ? "from QG" : "NOT replaced", battery_st ? "from QG" : "NOT replaced");
+ say("redwood: battery UI: shows%% %s, set-shows%% %s, bolt %s, chargePercent %s, setChargePercent %s, lockscreen ring %s\n",
+ original_battery_view_shows_percentage ? "ok" : "NO",
+ original_battery_view_set_shows_percentage ? "ok" : "NO",
+ original_battery_view_should_show_bolt ? "ok" : "NO",
+ original_battery_view_charge_percent ? "ok" : "NO",
+ original_battery_view_set_charge_percent ? "ok" : "NO",
+ original_ring_charging_level ? "ok" : "NO");
{
char args[1024] = "";
size_t size = sizeof(args) - 1;
@@ -542,6 +653,10 @@ redwood_springboard_init(void)
if (device && class_getInstanceMethod(object_getClass(device), monitoring)) {
((void (*)(id, SEL, BOOL))objc_msgSend)(device, monitoring, YES);
}
+ say("redwood: battery: SOC=%d%% battery=%d charger=%d charging=%d\n",
+ sysctl_int("kern.redwood.battery_soc"), sysctl_int("kern.redwood.battery_present"),
+ sysctl_int("kern.redwood.battery_charger_present"), sysctl_int("kern.redwood.battery_charging"));
+ battery_post_notifications();
});
notify_register_dispatch("com.redwood.unlock", &token, dispatch_get_main_queue(), ^(__unused int t) {
unlock();
diff --git a/xnu/files/bsd/dev/redwood_battery.c b/xnu/files/bsd/dev/redwood_battery.c
index a62bfb7..0171709 100644
--- a/xnu/files/bsd/dev/redwood_battery.c
+++ b/xnu/files/bsd/dev/redwood_battery.c
@@ -1,35 +1,11 @@
/*
- * Battery monitor for the POCO X5 Pro (redwood). This reads the live state-of-charge, battery
- * voltage, and battery current straight from the fuel-gauge peripheral inside PM7250B over
- * SPMI, so iOS can show real numbers in the status bar instead of a fake low-battery icon.
+ * Battery monitor for Xiaomi redwood (POCO X5 Pro).
*
- * Enabled only with boot-arg battery=1. Read-only: we do not touch the charger here, we do not
- * reconfigure any PMIC rail, we just poll a few registers and expose the result through
- * sysctl kern.redwood.battery_*. Writing to the charger is a separate project and a much
- * larger pile of code (sm8350-kernel-OKX ships ~3000 lines of qpnp-smb5 for that).
- *
- * Hardware layout: PM7250B lives at SPMI slave 2. The Qualcomm Gauge ("QG") peripheral is at
- * SPMI peripheral 0x48, so registers live at 0x48XX in that slave's address space. The
- * fuel-gauge chip ran its initial ADC burst back in Android before we yanked the system out
- * from under it, so its state-of-charge estimate is already current and keeps drifting with
- * use. We just read what the chip computed, we do not re-run the gauge algorithm. Register
- * meanings taken from drivers/power/supply/qcom/qg-reg.h in sm8350-kernel-OKX:
- *
- * QG_SOC_MONOTONIC (0xBF) -- 1 byte, 0..255 maps to 0..100 % (monotonic, i.e.
- * never ticks up on discharge, never down on charge;
- * this is what Android shows as "battery level")
- * QG_LAST_ADC_V_DATA (0xC0, 0xC1) -- 2 bytes LE, battery voltage in units of 194.6 uV
- * (max 12.75 V, we never go anywhere near that)
- * QG_LAST_ADC_I_DATA (0xC2, 0xC3) -- 2 bytes LE, battery current, signed, LSB is 61 uA
- * at the chip's default 5A range (negative = charging)
- *
- * SPMI itself is already set up by the buttons driver (redwood_buttons.c) which maps the PMIC
- * arbiter at step 1 and finds channels at step 2. We do the same two steps here independently
- * so this driver works even when buttons=0; it is a few kB of code to duplicate and it keeps
- * the two drivers from stepping on each other's state.
- *
- * Register layouts: Linux drivers/spmi/spmi-pmic-arb.c (arbiter version 5),
- * drivers/power/supply/qcom/qpnp-qg.c and qg-reg.h, and the phone's own pm7250b.dtsi.
+ * Redwood selects PM7325B and qcom,battery-charger over PMIC GLINK/ADSP, not
+ * the local PM7250B/QG layout. The resident UEFI contains the matching
+ * Qualcomm charger drivers. xnuloader owns that firmware context on core 0,
+ * polls it, and publishes a read-only cache; XNU only consumes that cache.
+ * Charger limits and JEITA protection therefore remain firmware-owned.
*/
#include <kern/clock.h>
#include <kern/thread.h>
@@ -41,244 +17,103 @@
#include <sys/systm.h>
extern vm_offset_t ml_io_map(vm_offset_t phys_addr, vm_size_t size);
-extern kern_return_t thread_terminate(thread_t thread);
-
-static inline uint32_t
-rd(vm_offset_t address)
-{
- return *(volatile uint32_t *)address;
-}
-
-static inline void
-wr(vm_offset_t address, uint32_t value)
-{
- *(volatile uint32_t *)address = value;
-}
-
-/* ---- PMIC arbiter (copied verbatim from redwood_buttons.c) ---------------------------------------- */
-
-#define ARB_CORE_PHYS 0x0c440000UL
-#define ARB_CORE_SIZE 0x1100UL
-#define ARB_CNFG_PHYS 0x0c40a000UL
-#define ARB_CNFG_SIZE 0x1000UL
-#define ARB_OBSERVER_PHYS 0x0e600000UL
-#define ARB_OBSERVER_SIZE 0x10000UL
-#define ARB_SYSTEM 0
-
-#define ARB_VERSION 0x000
-#define ARB_VERSION_5 0x50000000u
-#define ARB_VERSION_7 0x70000000u
-#define ARB_FEATURES 0x004
-#define ARB_CHANNEL(n) (0x900 + 4 * (n))
-#define ARB_OWNER(n) (0x700 + 4 * (n))
-#define ARB_MAX_CHANNELS 512
-
-#define OBSERVER_COMMAND 0x00
-#define OBSERVER_STATUS 0x08
-#define OBSERVER_DATA0 0x18
-#define OBSERVER_DATA1 0x1c
-#define STATUS_DONE 0x1
-#define STATUS_FAILURE 0x2
-#define STATUS_DENIED 0x4
-#define STATUS_DROPPED 0x8
-#define COMMAND_READ (1u << 27)
-#define READ_TIMEOUT_US 2000
-static vm_offset_t arb_core, arb_cnfg, arb_observer;
-
-struct peripheral {
- const char * name;
- uint16_t id; /* slave << 8 | peripheral: the high byte of its register addresses */
- uint16_t channel;
- bool found;
+#define LOADER_BATTERY_MAGIC 0x2154544142574452ULL
+#define LOADER_BATTERY_VERSION 1
+#define LOADER_BATTERY_VALID (1u << 0)
+#define LOADER_BATTERY_PRESENT (1u << 1)
+#define LOADER_BATTERY_CHARGER_PRESENT (1u << 2)
+#define LOADER_BATTERY_VOLTAGE_VALID (1u << 3)
+#define LOADER_BATTERY_CHARGING (1u << 4)
+#define LOADER_BATTERY_SOC_ESTIMATED (1u << 5)
+#define LOADER_PAGE_SIZE 0x4000UL
+
+struct loader_battery {
+ uint64_t magic;
+ uint32_t version, size;
+ volatile uint32_t sequence;
+ uint32_t flags;
+ int32_t soc_percent, voltage_mv, current_ma, temperature_c;
+ uint32_t charger_action, polls, failures, reserved;
};
-static struct peripheral qg = { .name = "the PM7250B fuel gauge (QG)", .id = 0x248 };
-/*
- * Read `count` (1 to 8) registers of a peripheral, from register `reg` on. Returns NULL on
- * success, or a short description of what went sideways. Verbatim copy of the buttons driver's
- * version -- the SPMI arbiter is the same regardless of what peripheral we poke at.
- */
-static const char *
-spmi_read(const struct peripheral *p, uint8_t reg, uint8_t *bytes, uint32_t count)
-{
- vm_offset_t channel = arb_observer + 0x10000UL * ARB_SYSTEM + 0x80UL * p->channel;
- uint64_t start = mach_absolute_time(), now, interval;
- uint32_t status, data[2];
- const char *problem = NULL;
+static volatile struct loader_battery *shared_battery;
+static int battery_soc_percent = -1, battery_voltage_mv = -1, battery_current_ma;
+static int battery_temperature_c = -273, battery_present, battery_charger_present;
+static int battery_charging, battery_soc_estimated, battery_charger_action = -1;
+static uint64_t battery_updates, battery_failures;
- nanoseconds_to_absolutetime(READ_TIMEOUT_US * (uint64_t)NSEC_PER_USEC, &interval);
- wr(channel + OBSERVER_COMMAND, COMMAND_READ | (uint32_t)reg << 4 | (count - 1));
- do {
- status = rd(channel + OBSERVER_STATUS);
- now = mach_absolute_time();
- } while (!(status & STATUS_DONE) && now - start < interval);
-
- if (!(status & STATUS_DONE)) {
- problem = "no answer from the arbiter";
- } else if (status & STATUS_DENIED) {
- problem = "the arbiter refuses";
- } else if (status & STATUS_FAILURE) {
- problem = "the transfer failed (nobody at that address?)";
- } else if (status & STATUS_DROPPED) {
- problem = "the transfer was dropped";
- } else {
- data[0] = rd(channel + OBSERVER_DATA0);
- data[1] = count > 4 ? rd(channel + OBSERVER_DATA1) : 0;
- for (uint32_t i = 0; i < count; i++) {
- bytes[i] = (uint8_t)(data[i / 4] >> (8 * (i % 4)));
- }
- }
- return problem;
-}
-
-/* Scan the arbiter's channel table for the SPMI peripheral we want to talk to. */
-static void
-find_channel(struct peripheral *p, uint32_t channels)
+SYSCTL_NODE(_kern, OID_AUTO, redwood, CTLFLAG_RW | CTLFLAG_LOCKED, 0, "redwood phone bits");
+SYSCTL_INT(_kern_redwood, OID_AUTO, battery_soc, CTLFLAG_RD | CTLFLAG_LOCKED,
+ &battery_soc_percent, 0, "battery state of charge, 0..100 percent, or -1 if unknown");
+SYSCTL_INT(_kern_redwood, OID_AUTO, battery_voltage_mv, CTLFLAG_RD | CTLFLAG_LOCKED,
+ &battery_voltage_mv, 0, "battery voltage in millivolts, or -1 if unknown");
+SYSCTL_INT(_kern_redwood, OID_AUTO, battery_current_ma, CTLFLAG_RD | CTLFLAG_LOCKED,
+ &battery_current_ma, 0, "battery current reported by Qualcomm firmware, in milliamps");
+SYSCTL_INT(_kern_redwood, OID_AUTO, battery_temperature_c, CTLFLAG_RD | CTLFLAG_LOCKED,
+ &battery_temperature_c, 0, "battery temperature reported by Qualcomm firmware, in Celsius");
+SYSCTL_INT(_kern_redwood, OID_AUTO, battery_present, CTLFLAG_RD | CTLFLAG_LOCKED,
+ &battery_present, 0, "1 if Qualcomm firmware reports a battery");
+SYSCTL_INT(_kern_redwood, OID_AUTO, battery_charger_present, CTLFLAG_RD | CTLFLAG_LOCKED,
+ &battery_charger_present, 0, "1 if Qualcomm firmware reports an attached charger");
+SYSCTL_INT(_kern_redwood, OID_AUTO, battery_charging, CTLFLAG_RD | CTLFLAG_LOCKED,
+ &battery_charging, 0, "1 while an attached charger is charging a non-full battery");
+SYSCTL_INT(_kern_redwood, OID_AUTO, battery_soc_estimated, CTLFLAG_RD | CTLFLAG_LOCKED,
+ &battery_soc_estimated, 0, "1 only when SOC had to be estimated from voltage");
+SYSCTL_INT(_kern_redwood, OID_AUTO, battery_charger_action, CTLFLAG_RD | CTLFLAG_LOCKED,
+ &battery_charger_action, 0, "last Qualcomm charger action, for diagnostics");
+
+static bool
+battery_snapshot(struct loader_battery *out)
{
- for (uint32_t n = 0; n < channels; n++) {
- uint32_t map = rd(arb_core + ARB_CHANNEL(n));
- uint32_t owner;
-
- if (map == 0 || ((map >> 8) & 0xfff) != p->id) {
- continue;
- }
- owner = rd(arb_cnfg + ARB_OWNER(n)) & 7;
- printf("battery: channel %u is %s (%#x); written by system %u\n", n, p->name, map, owner);
- if (!p->found || owner == ARB_SYSTEM) {
- p->found = true;
- p->channel = (uint16_t)n;
- }
- }
- if (!p->found) {
- printf("battery: no channel of the arbiter is %s (slave %u, peripheral %#x)\n",
- p->name, p->id >> 8, p->id & 0xff);
+ uint32_t before, after;
+
+ for (unsigned retry = 0; retry < 4; retry++) {
+ before = shared_battery->sequence;
+ if (before & 1) continue;
+ __asm__ volatile ("dmb ishld" ::: "memory");
+ memcpy(out, (const void *)(uintptr_t)shared_battery, sizeof(*out));
+ __asm__ volatile ("dmb ishld" ::: "memory");
+ after = shared_battery->sequence;
+ if (before == after && !(after & 1)) return true;
}
+ return false;
}
-/* ---- QG (fuel gauge) registers ---------------------------------------------------------------- */
-
-#define QG_PERIPH_OFFSET 0x00 /* registers 0x4800..0x48ff on PM7250B */
-#define QG_REG_PERPH_TYPE (QG_PERIPH_OFFSET + 0x04)
-#define QG_REG_PERPH_SUBTYPE (QG_PERIPH_OFFSET + 0x05)
-#define QG_REG_STATUS1 (QG_PERIPH_OFFSET + 0x08)
-#define QG_STATUS1_BATTERY_PRESENT (1u << 0)
-#define QG_STATUS1_QG_OK (1u << 7)
-#define QG_REG_SOC_MONOTONIC (QG_PERIPH_OFFSET + 0xbf)
-#define QG_REG_LAST_V_DATA_LSB (QG_PERIPH_OFFSET + 0xc0)
-#define QG_REG_LAST_I_DATA_LSB (QG_PERIPH_OFFSET + 0xc2)
-#define QG_TYPE 0x0d /* vendor PERPH_TYPE_QG */
-
-/* Live state. These are updated by battery_thread and read by the sysctls below. */
-static int battery_soc_percent = -1; /* 0..100, or -1 if we have not read it yet */
-static int battery_voltage_mv = -1; /* millivolts; signed only because -1 means unknown */
-static int battery_current_ma = 0; /* negative = charging, positive = discharging, mA */
-static int battery_present = 0; /* 1 if a battery is wired up, 0 otherwise */
-static uint64_t battery_updates; /* how many successful poll cycles so far */
-static uint64_t battery_failures; /* and failed ones */
-
-/*
- * One polling cycle: read SOC, voltage, current, status. Each read is one SPMI transaction and
- * takes a few tens of microseconds, so the whole thing is fast enough that we can do it every
- * couple of seconds without noticing it in the system's cpu budget.
- */
static void
battery_poll(void)
{
- uint8_t soc = 0, status = 0, v[2] = { 0 }, i[2] = { 0 };
- const char *problem;
+ struct loader_battery sample;
- problem = spmi_read(&qg, QG_REG_STATUS1, &status, 1);
- if (problem != NULL) {
+ if (!battery_snapshot(&sample) || sample.magic != LOADER_BATTERY_MAGIC ||
+ sample.version != LOADER_BATTERY_VERSION || sample.size < sizeof(sample) ||
+ !(sample.flags & LOADER_BATTERY_VALID) || sample.soc_percent < 0 || sample.soc_percent > 100) {
battery_failures++;
return;
}
- battery_present = (status & QG_STATUS1_BATTERY_PRESENT) ? 1 : 0;
-
- problem = spmi_read(&qg, QG_REG_SOC_MONOTONIC, &soc, 1);
- if (problem == NULL) {
- battery_soc_percent = (int)soc * 100 / 255; /* the chip packs 0..100 % into 0..255 */
- }
-
- problem = spmi_read(&qg, QG_REG_LAST_V_DATA_LSB, v, 2);
- if (problem == NULL) {
- /* 2-byte LE raw ADC code; the chip's step is 194.6 uV per LSB. Keep math in integers:
- * 1946 * raw / 10000 is millivolts. Range of a healthy single-cell li-ion is 3000..4400. */
- uint32_t raw = (uint32_t)v[0] | ((uint32_t)v[1] << 8);
- battery_voltage_mv = (int)((1946u * raw) / 10000u);
- }
-
- problem = spmi_read(&qg, QG_REG_LAST_I_DATA_LSB, i, 2);
- if (problem == NULL) {
- /* 2-byte LE SIGNED raw ADC code; LSB is 61 uA at the 5A range. Negative value = current
- * is flowing INTO the battery (charging), positive = flowing OUT (discharging). */
- int16_t raw = (int16_t)((uint16_t)i[0] | ((uint16_t)i[1] << 8));
- battery_current_ma = (int)(((int32_t)raw * 61) / 1000);
- }
-
+ battery_soc_percent = sample.soc_percent;
+ battery_voltage_mv = (sample.flags & LOADER_BATTERY_VOLTAGE_VALID) ? sample.voltage_mv : -1;
+ battery_current_ma = sample.current_ma;
+ battery_temperature_c = sample.temperature_c;
+ battery_present = (sample.flags & LOADER_BATTERY_PRESENT) != 0;
+ battery_charger_present = (sample.flags & LOADER_BATTERY_CHARGER_PRESENT) != 0;
+ battery_charging = (sample.flags & LOADER_BATTERY_CHARGING) != 0;
+ battery_soc_estimated = (sample.flags & LOADER_BATTERY_SOC_ESTIMATED) != 0;
+ battery_charger_action = (int)sample.charger_action;
battery_updates++;
}
-/* ---- sysctl: kern.redwood.battery_* --------------------------------------------------------- */
-
-SYSCTL_NODE(_kern, OID_AUTO, redwood, CTLFLAG_RW | CTLFLAG_LOCKED, 0, "redwood phone bits");
-SYSCTL_INT(_kern_redwood, OID_AUTO, battery_soc, CTLFLAG_RD | CTLFLAG_LOCKED,
- &battery_soc_percent, 0, "battery state of charge, 0..100 percent, or -1 if unknown");
-SYSCTL_INT(_kern_redwood, OID_AUTO, battery_voltage_mv, CTLFLAG_RD | CTLFLAG_LOCKED,
- &battery_voltage_mv, 0, "battery voltage in millivolts, or -1 if unknown");
-SYSCTL_INT(_kern_redwood, OID_AUTO, battery_current_ma, CTLFLAG_RD | CTLFLAG_LOCKED,
- &battery_current_ma, 0, "battery current in milliamps (negative = charging)");
-SYSCTL_INT(_kern_redwood, OID_AUTO, battery_present, CTLFLAG_RD | CTLFLAG_LOCKED,
- &battery_present, 0, "1 if the fuel gauge sees a battery wired up, 0 otherwise");
-
-/* ---- all of it together ---------------------------------------------------------------------- */
-
static void
battery_thread(__unused void *parameter, __unused wait_result_t result)
{
- uint32_t version, channels;
- uint8_t subtype;
- const char *problem;
-
- printf("battery: step 1: map the PMIC arbiter and read its version\n");
- arb_core = ml_io_map(ARB_CORE_PHYS, ARB_CORE_SIZE);
- arb_cnfg = ml_io_map(ARB_CNFG_PHYS, ARB_CNFG_SIZE);
- arb_observer = ml_io_map(ARB_OBSERVER_PHYS, ARB_OBSERVER_SIZE);
- version = rd(arb_core + ARB_VERSION);
- channels = rd(arb_core + ARB_FEATURES) & 0x7ff;
- printf("battery: PMIC arbiter version %#x, %u channels\n", version, channels);
- if (version < ARB_VERSION_5 || version >= ARB_VERSION_7) {
- printf("battery: this driver knows version 5 of the arbiter only; giving up\n");
- thread_terminate(current_thread());
- return;
- }
- if (channels == 0 || channels > ARB_MAX_CHANNELS) {
- channels = ARB_MAX_CHANNELS;
- }
-
- printf("battery: step 2: find the fuel-gauge channel in the arbiter\n");
- find_channel(&qg, channels);
- if (!qg.found) {
- thread_terminate(current_thread());
- return;
- }
-
- printf("battery: step 3: confirm the fuel-gauge peripheral is where we think it is\n");
- problem = spmi_read(&qg, QG_REG_PERPH_TYPE, &subtype, 1);
- if (problem != NULL) {
- printf("battery: cannot read the peripheral type (%s)\n", problem);
- thread_terminate(current_thread());
- return;
- }
- printf("battery: peripheral type is %#x (expected %#x for the fuel gauge)\n", subtype, QG_TYPE);
-
- printf("battery: step 4: polling every 5 seconds\n");
for (unsigned tick = 0;; tick++) {
battery_poll();
- if (tick % 12 == 0) { /* once a minute at 5-second cadence */
- printf("battery: SOC=%d%%, V=%d mV, I=%d mA, %spresent (%llu reads, %llu failed)\n",
- battery_soc_percent, battery_voltage_mv, battery_current_ma,
- battery_present ? "" : "not ", battery_updates, battery_failures);
+ if (tick % 12 == 0) {
+ printf("battery: SOC=%d%%%s, V=%d mV, I=%d mA, T=%d C, battery=%d charger=%d charging=%d "
+ "action=%d (%llu reads, %llu failed)\n", battery_soc_percent,
+ battery_soc_estimated ? " (voltage estimate)" : "", battery_voltage_mv,
+ battery_current_ma, battery_temperature_c, battery_present, battery_charger_present,
+ battery_charging, battery_charger_action, battery_updates, battery_failures);
}
delay_for_interval(5000, NSEC_PER_MSEC);
}
@@ -288,12 +123,23 @@ void
redwood_battery_init(void)
{
uint32_t enabled = 0;
+ uint64_t shared_pa = 0;
+ vm_offset_t page;
thread_t thread;
- if (!PE_parse_boot_argn("battery", &enabled, sizeof(enabled)) || !enabled) {
+ if (!PE_parse_boot_argn("battery", &enabled, sizeof(enabled)) || !enabled) return;
+ if (!PE_parse_boot_argn("battery_shared_pa", &shared_pa, sizeof(shared_pa)) || shared_pa == 0) {
+ printf("battery: xnuloader did not provide battery_shared_pa\n");
return;
}
- if (kernel_thread_start(battery_thread, NULL, &thread) == KERN_SUCCESS) {
- thread_deallocate(thread);
+ page = ml_io_map((vm_offset_t)(shared_pa & ~(LOADER_PAGE_SIZE - 1)), LOADER_PAGE_SIZE);
+ shared_battery = (volatile struct loader_battery *)(page + (shared_pa & (LOADER_PAGE_SIZE - 1)));
+ if (shared_battery->magic != LOADER_BATTERY_MAGIC || shared_battery->version != LOADER_BATTERY_VERSION ||
+ shared_battery->size < sizeof(struct loader_battery)) {
+ printf("battery: bad xnuloader cache at %#llx (magic %#llx version %u size %u)\n", shared_pa,
+ shared_battery->magic, shared_battery->version, shared_battery->size);
+ return;
}
+ printf("battery: Qualcomm UEFI cache at pa %#llx; polling every 5 seconds\n", shared_pa);
+ if (kernel_thread_start(battery_thread, NULL, &thread) == KERN_SUCCESS) thread_deallocate(thread);
}
diff --git a/xnu/files/bsd/sys/redwood_input.h b/xnu/files/bsd/sys/redwood_input.h
index 0c58317..ec5a96f 100644
--- a/xnu/files/bsd/sys/redwood_input.h
+++ b/xnu/files/bsd/sys/redwood_input.h
@@ -41,8 +41,8 @@ void redwood_touch_post(uint32_t type, uint32_t finger, uint32_t x, uint32_t y);
void redwood_input_init(void);
void redwood_virtio_input_init(void); /* boot-arg virtio_input=1: QEMU's virtio pointer and keyboard */
void redwood_goodix_init(void); /* boot-arg touchscreen=1: the phone's Goodix or FocalTech touch screen */
-void redwood_battery_init(void); /* boot-arg battery=1: SOC/voltage/current from PM7250B fuel gauge */
void redwood_buttons_init(void); /* boot-arg buttons=1: the phone's power and volume keys */
+void redwood_battery_init(void); /* boot-arg battery=1: Qualcomm UEFI charger state shared by xnuloader */
void redwood_net_init(void); /* en0, if there is something to carry its frames: sys/redwood_net.h */
void redwood_cpufreq_init(void); /* boot-arg cpufreq=1: the phone's processor at full speed */
void redwood_adreno_init(void); /* boot-arg adreno=N: the first N steps of waking the phone's GPU */
diff --git a/xnuloader/efi.h b/xnuloader/efi.h
index 808678b..670b134 100644
--- a/xnuloader/efi.h
+++ b/xnuloader/efi.h
@@ -120,6 +120,36 @@ typedef struct efi_gop {
efi_gop_mode *mode;
} efi_gop;
+/*
+ * Qualcomm's open ChargerEx protocol. The protocol is implemented by the
+ * stock ChargerExDxe in the redwood UEFI image and is safe to use for
+ * read-only charger/battery queries while boot services remain resident.
+ */
+typedef struct {
+ uint64_t revision;
+ efi_status (EFIAPI *get_charger_presence)(uint8_t *present);
+ efi_status (EFIAPI *get_battery_presence)(uint8_t *present);
+ efi_status (EFIAPI *get_battery_voltage)(uint32_t *millivolts);
+ efi_status (EFIAPI *is_offmode_charging)(uint8_t *offmode);
+ efi_status (EFIAPI *is_power_ok)(uint32_t type, void *info);
+} efi_charger_ex;
+
+/*
+ * The first public entries of Xiaomi's Qualcomm charger protocol. Its GUID,
+ * revision and function layout are taken from the QcomChargerDxeLA.efi and
+ * QcomChargerApp.efi shipped in this repository. get_charging_action is used
+ * only as a status read: the loader never calls take_action and therefore does
+ * not change current/voltage limits or bypass the firmware's JEITA checks.
+ */
+typedef struct {
+ uint64_t revision;
+ efi_status (EFIAPI *initialize)(uint8_t enable);
+ void *reserved_10;
+ void *reserved_18;
+ efi_status (EFIAPI *get_charging_action)(uint32_t *action, void *battery_status, uint8_t display);
+ efi_status (EFIAPI *take_action)(uint32_t action, const void *battery_status);
+} efi_qcom_charger;
+
/* Block I/O + Partition Info (used to persist the log on the phone) */
typedef struct {
uint32_t media_id; uint8_t removable, present, logical, read_only, write_caching; uint8_t pad[3];
@@ -202,5 +232,7 @@ typedef struct {
#define EFI_GOP_GUID { 0x9042a9de, 0x23dc, 0x4a38, { 0x96, 0xfb, 0x7a, 0xde, 0xd0, 0x80, 0x51, 0x6a } }
#define EFI_BLOCK_IO_GUID { 0x964e5b21, 0x6459, 0x11d2, { 0x8e, 0x39, 0x00, 0xa0, 0xc9, 0x69, 0x72, 0x3b } }
#define EFI_PARTITION_INFO_GUID { 0x8cf2f62c, 0xbc9b, 0x4821, { 0x80, 0x8d, 0xec, 0x9e, 0xc4, 0x21, 0xa1, 0xa0 } }
+#define QCOM_CHARGER_EX_GUID { 0x6edc8a6d, 0x2663, 0x43cd, { 0x90, 0xff, 0x46, 0x21, 0xff, 0xd1, 0x0d, 0xf5 } }
+#define QCOM_CHARGER_GUID { 0x21bfb78b, 0x02ea, 0x4ab7, { 0x99, 0x40, 0xb5, 0x8a, 0x2a, 0xd6, 0x58, 0x29 } }
#endif /* XNULOADER_EFI_H */
diff --git a/xnuloader/loader.c b/xnuloader/loader.c
index 9a03183..4d542cd 100644
--- a/xnuloader/loader.c
+++ b/xnuloader/loader.c
@@ -783,6 +783,64 @@ struct kernel_live {
uint64_t stop; /* ours to write: the host wants the run to end ("oem stop") */
};
+/*
+ * Battery state shared with XNU. Qualcomm's charger firmware remains a UEFI
+ * boot-service client on core 0 while XNU runs on another core. The loader is
+ * therefore the one safe place to call it; XNU only reads this cache.
+ */
+#define LOADER_BATTERY_MAGIC 0x2154544142574452ULL /* "RDWBATT!" */
+#define LOADER_BATTERY_VERSION 1
+#define LOADER_BATTERY_VALID (1u << 0)
+#define LOADER_BATTERY_PRESENT (1u << 1)
+#define LOADER_BATTERY_CHARGER_PRESENT (1u << 2)
+#define LOADER_BATTERY_VOLTAGE_VALID (1u << 3)
+#define LOADER_BATTERY_CHARGING (1u << 4)
+#define LOADER_BATTERY_SOC_ESTIMATED (1u << 5)
+
+struct loader_battery {
+ uint64_t magic;
+ uint32_t version, size;
+ volatile uint32_t sequence;
+ uint32_t flags;
+ int32_t soc_percent;
+ int32_t voltage_mv;
+ int32_t current_ma;
+ int32_t temperature_c;
+ uint32_t charger_action;
+ uint32_t polls;
+ uint32_t failures;
+ uint32_t reserved;
+};
+
+/*
+ * Layout returned by QcomChargerDxeLA's GetChargingAction, word offsets confirmed by dumping the
+ * raw buffer on this phone: [2] state of charge, [3] charge current (signed mA), [4] battery
+ * voltage (mV), [5] temperature (C). `rest` is oversized on purpose so a firmware that writes
+ * more than the fields we name cannot smash the stack.
+ */
+struct qcom_battery_status {
+ uint32_t status0, status1;
+ int32_t state_of_charge;
+ int32_t charge_current_ma;
+ int32_t battery_voltage_mv;
+ int32_t battery_temperature_c;
+ uint32_t rest[26];
+};
+
+static struct loader_battery charger_shared __attribute__((aligned(0x2000))) = {
+ .magic = LOADER_BATTERY_MAGIC,
+ .version = LOADER_BATTERY_VERSION,
+ .size = sizeof(struct loader_battery),
+ .soc_percent = -1,
+ .voltage_mv = -1,
+ .current_ma = 0,
+ .temperature_c = -273,
+ .charger_action = 0xffffffffu,
+};
+static efi_charger_ex *CHARGER_EX;
+static efi_qcom_charger *QCOM_CHARGER;
+static int charger_enabled;
+
/* The screen recording the last kernel left in memory, if it made one ("oem recording" stages it). */
static const char *recording_data;
static uint64_t recording_len;
@@ -889,6 +947,122 @@ static void print(const char *fmt, ...);
static void flush_log(void);
static void put_char(char c);
+/* A voltage-only fallback is deliberately conservative; the real QG SOC wins whenever available. */
+static int
+soc_from_voltage(uint32_t mv)
+{
+ if (mv <= 3300) return 0;
+ if (mv < 3700) return (int)((mv - 3300) * 10 / 400);
+ if (mv < 3900) return 10 + (int)((mv - 3700) * 40 / 200);
+ if (mv < 4200) return 50 + (int)((mv - 3900) * 40 / 300);
+ if (mv < 4400) return 90 + (int)((mv - 4200) * 10 / 200);
+ return 100;
+}
+
+/* Read-only status query. No TakeAction call and no charger-limit writes are made here. */
+static void
+charger_poll(void)
+{
+ struct qcom_battery_status status;
+ uint32_t action = 0xffffffffu, flags = 0, voltage = 0;
+ uint8_t present = 0, plugged = 0;
+ efi_status s;
+ int have_soc = 0, have_voltage = 0;
+
+ if (!charger_enabled) {
+ return;
+ }
+ memset(&status, 0, sizeof(status));
+
+ charger_shared.sequence++;
+ __asm__ volatile ("dmb ish" ::: "memory");
+ if (CHARGER_EX != NULL) {
+ s = CHARGER_EX->get_battery_presence(&present);
+ if (!EFI_ERROR(s) && present) flags |= LOADER_BATTERY_PRESENT;
+ s = CHARGER_EX->get_charger_presence(&plugged);
+ if (!EFI_ERROR(s) && plugged) flags |= LOADER_BATTERY_CHARGER_PRESENT;
+ s = CHARGER_EX->get_battery_voltage(&voltage);
+ if (!EFI_ERROR(s) && voltage >= 2500 && voltage <= 5000) {
+ have_voltage = 1;
+ flags |= LOADER_BATTERY_VOLTAGE_VALID;
+ charger_shared.voltage_mv = (int32_t)voltage;
+ }
+ }
+ if (QCOM_CHARGER != NULL) {
+ s = QCOM_CHARGER->get_charging_action(&action, &status, 0);
+ if (!EFI_ERROR(s)) {
+ charger_shared.charger_action = action;
+ if (status.battery_voltage_mv >= 2500 && status.battery_voltage_mv <= 5000) {
+ charger_shared.voltage_mv = status.battery_voltage_mv;
+ have_voltage = 1;
+ flags |= LOADER_BATTERY_VOLTAGE_VALID;
+ }
+ /*
+ * GetChargingAction returns the firmware's state of charge in word [2] of its
+ * output (confirmed by dumping the raw buffer on this device). We use it as-is
+ * EXCEPT when the fuel gauge is clearly not ready yet: right after initialize()
+ * it can report a near-empty SOC while the cell voltage says otherwise, which is
+ * where the old "shows 1% on a half-full battery" came from. A Li-ion cell above
+ * ~3.6 V is never under 5%, so in that case we distrust the gauge and fall back to
+ * the voltage estimate below until it settles.
+ */
+ if (status.state_of_charge >= 0 && status.state_of_charge <= 100) {
+ int vmv = have_voltage ? charger_shared.voltage_mv : 0;
+ if (status.state_of_charge < 5 && vmv > 3600) {
+ /* fuel gauge not settled; leave it to the voltage fallback */
+ } else {
+ charger_shared.soc_percent = status.state_of_charge;
+ have_soc = 1;
+ flags |= LOADER_BATTERY_VALID;
+ }
+ }
+ charger_shared.current_ma = status.charge_current_ma;
+ charger_shared.temperature_c = status.battery_temperature_c;
+ }
+ }
+ if (!have_soc && have_voltage) {
+ charger_shared.soc_percent = soc_from_voltage((uint32_t)charger_shared.voltage_mv);
+ flags |= LOADER_BATTERY_VALID | LOADER_BATTERY_SOC_ESTIMATED;
+ }
+ if (plugged && charger_shared.soc_percent < 100) {
+ flags |= LOADER_BATTERY_CHARGING;
+ }
+ charger_shared.flags = flags;
+ charger_shared.polls++;
+ if (!(flags & LOADER_BATTERY_VALID)) charger_shared.failures++;
+ __asm__ volatile ("dmb ish" ::: "memory");
+ charger_shared.sequence++;
+ clean_dcache((uint64_t)(uintptr_t)&charger_shared,
+ (uint64_t)(uintptr_t)&charger_shared + sizeof(charger_shared));
+}
+
+static void
+charger_start(void)
+{
+ static const efi_guid charger_ex_guid = QCOM_CHARGER_EX_GUID;
+ static const efi_guid qcom_charger_guid = QCOM_CHARGER_GUID;
+ efi_status ex_status, qcom_status, init_status = 0;
+
+ if (charger_enabled || PLAT != &platform_redwood) {
+ return;
+ }
+ ex_status = BS->locate_protocol(&charger_ex_guid, NULL, (void **)&CHARGER_EX);
+ qcom_status = BS->locate_protocol(&qcom_charger_guid, NULL, (void **)&QCOM_CHARGER);
+ if (!EFI_ERROR(qcom_status) && QCOM_CHARGER != NULL) {
+ /* Re-open the driver's status path after QcomChargerApp handed off to the OS. */
+ init_status = QCOM_CHARGER->initialize(1);
+ }
+ charger_enabled = (!EFI_ERROR(ex_status) && CHARGER_EX != NULL) ||
+ (!EFI_ERROR(qcom_status) && QCOM_CHARGER != NULL);
+ print("battery: ChargerEx=%x QcomCharger=%x init=%x shared=%x\n", ex_status, qcom_status, init_status,
+ (uint64_t)(uintptr_t)&charger_shared);
+ charger_poll();
+ print("battery: flags=%x SOC=%d%% V=%d mV I=%d mA T=%d C action=%d\n", (uint64_t)charger_shared.flags,
+ (uint64_t)(int64_t)charger_shared.soc_percent, (uint64_t)(int64_t)charger_shared.voltage_mv,
+ (uint64_t)(int64_t)charger_shared.current_ma, (uint64_t)(int64_t)charger_shared.temperature_c,
+ (uint64_t)charger_shared.charger_action);
+}
+
/* For the record: how much the kernel on the other core has logged so far, and the end of it. */
static void
note_kernel_progress(const struct kernel_live *live, uint64_t slide, uint64_t seconds)
@@ -951,7 +1125,7 @@ static int64_t
run_on_other_cpu(uint64_t mpidr, uint64_t entry, uint64_t boot_args_pa, const struct kernel_live *live, uint64_t slide,
uint64_t budget)
{
- uint64_t daif = 0, group1 = 0, started, last_pet, last_beat_at, last_beat = 0, noted = 0;
+ uint64_t daif = 0, group1 = 0, started, last_pet, last_battery, last_beat_at, last_beat = 0, noted = 0;
int64_t status;
int arrived = 0, watching_only = 0;
@@ -973,7 +1147,7 @@ run_on_other_cpu(uint64_t mpidr, uint64_t entry, uint64_t boot_args_pa, const st
/* the last argument arrives in that core's x0 */
status = psci_call(PSCI_CPU_ON, mpidr, (uint64_t)(uintptr_t)&other_cpu_entry, (uint64_t)(uintptr_t)&other_cpu_start);
- started = last_pet = last_beat_at = READ_SYSREG(CNTVCT_EL0);
+ started = last_pet = last_battery = last_beat_at = READ_SYSREG(CNTVCT_EL0);
print("core %x: the firmware answers %d to the request to start it\n", mpidr, (uint64_t)status);
if (PLAT->log_other_cpu) {
flush_log();
@@ -1030,6 +1204,11 @@ run_on_other_cpu(uint64_t mpidr, uint64_t entry, uint64_t boot_args_pa, const st
wdt_pet();
last_pet = READ_SYSREG(CNTVCT_EL0);
}
+ /* Qualcomm's UEFI charger stack owns the PMIC/GLINK side; keep XNU's cache live. */
+ if (charger_enabled && seconds_since(last_battery) >= 5) {
+ charger_poll();
+ last_battery = READ_SYSREG(CNTVCT_EL0);
+ }
if (arrived && live != NULL) {
if (live->heartbeat != last_beat) {
last_beat = live->heartbeat;
@@ -1518,12 +1697,17 @@ run_kernel(int have_checkpoint, uint64_t checkpoint)
/* "bootcpu=N" among the kernel's arguments: run it on core N (see run_on_other_cpu) */
uint64_t this_mpidr = READ_SYSREG(MPIDR_EL1) & 0xFFFFFF, kernel_mpidr = this_mpidr;
/* "cpus=N" or "cpumask=M" there too: the kernel may have more cores than the one it starts on */
- int more_cores = 0;
+ int more_cores = 0, battery_requested = 0;
for (const char *c = pi.cmdline; *c; c++) {
if (starts_with(c, "bootcpu=") && (c == pi.cmdline || c[-1] == ' ') && c[8] >= '0' && c[8] <= '7') {
kernel_mpidr = (uint64_t)(c[8] - '0') << PLAT->core_shift;
}
more_cores |= (starts_with(c, "cpus=") || starts_with(c, "cpumask=")) && (c == pi.cmdline || c[-1] == ' ');
+ battery_requested |= starts_with(c, "battery=1") && (c == pi.cmdline || c[-1] == ' ') &&
+ (c[9] == 0 || c[9] == ' ');
+ }
+ if (battery_requested) {
+ charger_start();
}
if (checkpoint == CHECKPOINT_SELFTEST) {
kernel_mpidr = this_mpidr;
@@ -1627,6 +1811,19 @@ run_kernel(int have_checkpoint, uint64_t checkpoint)
ba->commandLine[n++] = *c;
}
}
+ if (battery_requested) {
+ static const char prefix[] = "battery_shared_pa=0x";
+ static const char hex[] = "0123456789abcdef";
+ uint64_t address = (uint64_t)(uintptr_t)&charger_shared;
+
+ for (const char *c = prefix; *c && n < BOOT_LINE_LENGTH - 1; c++) {
+ ba->commandLine[n++] = *c;
+ }
+ for (int shift = 60; shift >= 0 && n < BOOT_LINE_LENGTH - 1; shift -= 4) {
+ ba->commandLine[n++] = hex[(address >> shift) & 0xf];
+ }
+ if (n < BOOT_LINE_LENGTH - 1) ba->commandLine[n++] = ' ';
+ }
for (const char *c = pi.cmdline; *c && n < BOOT_LINE_LENGTH - 1; c++) {
ba->commandLine[n++] = *c;
}