Skip to content

Conversation

me-cafebabe
Copy link
Contributor

Please check out each commits for details.

@me-cafebabe me-cafebabe changed the title (This PR contains multiple different changes) Better Android support, plus a bit of improvements Apr 11, 2025
@me-cafebabe me-cafebabe force-pushed the main branch 2 times, most recently from 71bdb96 to 23a12f5 Compare April 13, 2025 18:25
Copy link
Member

@stephan-gh stephan-gh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot for working on this! I have already pushed 5 of your commits that look good to me. Please rebase to drop them from the branch.

I haven't fully understood the "big picture" here. Could you maybe add a short "design document" to Documentation/android.md (or something like this) and describe:

  • Motivation (Boot Android with mainline through lk2nd)
  • How to set this up (flash lk2nd to both boot and recovery)
  • How do the expected flows look like? Android sets up the recovery message, reboots to recovery, the stock bootloader reads the recovery message, boots the recovery partition (= lk2nd) and then lk2nd does the same again?

I think understanding this better would help me to review the rest of the changes.

boot_into_recovery = 1;
}

#if !ABOOT_STANDALONE
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have you tried

if (IS_ENABLED(ABOOT_STANDALONE))
	goto out;

(Not sure if that code causes compile errors...)

Rule of thumb: Avoid macros where possible, this improves compile testing. I'm not saying goto is great, but the jump label is there already, so not our fault. >:D

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yep, some of those code should cause compilation errors...

as far as I can see, reset_device_info() and set_device_root() functions are already excluded by macro !ABOOT_STANDALONE in commit app: aboot: Avoid using special partitions using ABOOT_STANDALONE=1, and those code is calling that two functions

@me-cafebabe
Copy link
Contributor Author

Thanks a lot for working on this! I have already pushed 5 of your commits that look good to me. Please rebase to drop them from the branch.

I haven't fully understood the "big picture" here. Could you maybe add a short "design document" to Documentation/android.md (or something like this) and describe:

* Motivation (Boot Android with mainline through lk2nd)

* How to set this up (flash lk2nd to both boot and recovery)

* How do the expected flows look like? Android sets up the recovery message, reboots to recovery, the stock bootloader reads the recovery message, boots the recovery partition (= lk2nd) and then lk2nd does the same again?

I think understanding this better would help me to review the rest of the changes.

Sorry, I'm unlikely able to write a decent document, but I can explain my "big picture" to you:

Honestly, I have to use lk2nd only because of the mainline kernel ports relies on it. Integrating lk2nd in Android and inserting lk2nd to the boot flow is breaking Android standards and is making the things a bit complicated.

I'm trying to make lk2nd transparent to the end user. Ideally, the end user doesn't have to worry about the existence of lk2nd.

In my own Android device tree configuration, I have added build rules to build lk2nd inline, and have overridden boot.img build rule to have it placing lk2nd.img at 0 offset and the real Android boot.img at 512KB offset.

lk2nd is integrated into recovery.img too, in the same way as boot.img. When booting into recovery, the boot flow is:

stock bootloader detects boot mode is recovery -> stock bootloader boots recovery partition at 0 offset -> lk2nd is loaded because it's at 0 offset -> lk2nd keeps detecting boot mode is recovery -> lk2nd boots the virtual partition named "recovery" which is actually 512KB offset of the real recovery partition -> Android recovery image is loaded

There's two ways to set the boot mode for the next boot: writing string to misc partition (standard Android way), and the stuff inside #if USE_PON_REBOOT_REG.

When the users wants to install android with mainline kernel, they could either get partition images for flashing with fastboot, or standard Android OTA package for flashing in recovery mode, either ones have {boot,recovery}.img with lk2nd embedded in.

The only things that still haven't got dealt are:

  • Flashing {boot,recovery}.img through lk2nd's fastboot. The data on the real recovery partition will be like "lk2nd + lk2nd + recovery".
  • Booting {boot,recovery}.img through fastboot boot. The device will boot lk2nd from fastboot client, however then the lk2nd will not load the real Android boot image from memory correspondingly, but loading from mmc instead.

Useful for Android recovery that runs mainline kernel
If it's not detecting boot image header at 512 KiB offset, try
booting from 0 offset.

Useful when lk2nd boots the recovery partition, at least.
Android could run on mainline kernel...

Fix up some logic and code style in `_emmc_recovery_init()` while at it.
This is solely used for Android OS, but shouldn't cause any problem
to Linux OS.

This may cause problem for devices that was launched with Android 9
and earlier and wants to boot Android from lk2nd. I'll mitigate this
in the following commits.
For Linux OS, this has no use.

For custom Android OS, the developers are used to make changes
on the source files directly, rather than relying on this logic.

Additionally, the changes that it make are most likely deprecated
on newer Android versions or newer kernel versions.

Stock Android OS may rely on it though, but the purpose of lk2nd
is likely never used to be booting stock Android OS...

Change-Id: Ia0f331dc60c7d2c023c0d79e531e801bc6193d55
Code copied from boot_linux_from_mmc()

Change-Id: I8c6dd64eabd264f42e13e443bceae6e5cc525a38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

4 participants