Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

twrp: Initial basic support for TWRP builds #99

Draft
wants to merge 6 commits into
base: master
Choose a base branch
from

Conversation

samueldr
Copy link
Contributor

@samueldr samueldr commented Apr 25, 2021

Read the commits.

TLDR:

  • builds a recovery.img
  • on a limited sample size of one device tree, not even known working, does not work
  • Known to work with limited scope

I guess next steps could be:

I don't know if I'll get back to this. It was mostly done so I could try and build the output of the TWRP Device Tree Generator thing, and it ended up not working for the device I tried it with.


Known issues

  • Only cares for twrp-9.0 (android 9.0) TWRP
  • No handling of device trees; "BYODT", or bring your own DT...

Reading material

That last thread with weird info has this bit:

Before doing the repo sync, we will need a few extra repositories to be downloaded. Create a subfolder .repo/local_manifests and create an XML file (e.g. custom.xml) with the following content:

and

Additionally, we will need some driver binaries that are available from Google. Get the Google (Qcom is not necessary) drivers for the latest Pie release here:

So according to this old, yet updated thread, it looks like it's not turn-key. There may be some manual stuff to do to have upstream trees built.


Sample known working config
#
# Usage
# =====
#
# ```
# $ nix-build --arg configuration ./sofiar-twrp.nix -A config.build.twrp
# ```
#

{ config, pkgs, lib, ... }:

let
  inherit (pkgs) fetchFromGitHub;
in
{
  device = "sofiar";
  androidVersion = 10;
  flavor = "twrp";
  source.dirs = {
    "kernel/motorola/trinket" = {
      src = fetchFromGitHub {
        owner = "moto-sm6xxx";
        repo = "android_kernel_motorola_trinket";
        rev = "dc06278614038a6569650168dd1099f48fae1ebe";
        sha256 = "0pmls0nxlmb2jbfxngpkdw2i20rrxjq7an8qar8cidhf10sk4mvj";
      };
    };
    "device/motorola/sofiar" = {
      src = fetchFromGitHub {
        owner = "moto-sm6xxx";
        repo = "android_device_motorola_sofiar";
        rev = "ece39f82849b3e50b0a1ebbdc689265abeb7b6e0";
        sha256 = "1x0g63h4ar7zf6dspz339qri7hkxsgzb45h26xk45g48c1sv5wzm";
      };
    };
  };
}

@danielfullmer
Copy link
Collaborator

I've just tried this out and I've got it working on marlin (Pixel 1 XL) with the following configuration:

{ pkgs, ... }:

{
  flavor = "twrp";
  device = "marlin";

  # At least marlin needs this... https://github.com/minimal-manifest-twrp/platform_manifest_twrp_omni/issues/33
  envVars.ALLOW_MISSING_DEPENDENCIES = "true";

  # https://forum.xda-developers.com/t/how-to-building-twrp-from-source-with-goodies.4029449/
  source.dirs."device/google/marlin".src = pkgs.fetchFromGitHub {
    owner = "TeamWin";
    repo = "android_device_google_marlin";
    rev = "72c4888ced18ffdbff962a3916cbd2a830f4c1ef";
    sha256 = "06lf0p8gcm5ixsn08j54k79v55g1akaliyvjsws0v947kb4cs9md";
  };
  source.dirs."hardware/qcom/bootctrl".src = pkgs.fetchgit {
    url = "https://android.googlesource.com/platform/hardware/qcom/bootctrl";
    rev = "android-9.0.0_r61";
    sha256 = "0zm2kqxk7pbn5ihbx3gz0w9ss28qjaxrcfcfx6s4l8gnn0afzk0j";
  };
  source.dirs."hardware/qcom/msm8998".src = pkgs.fetchgit {
    url = "https://android.googlesource.com/platform/hardware/qcom/msm8998";
    rev = "android-9.0.0_r61";
    sha256 = "0qr2mjqwg947hlwvgy511ay4acziyclxz1l52dg2vgsndqhb9hmr";
  };

Built with: nix-build --arg configuration ./marlin-twrp.nix -A config.build.twrp

As @samueldr mentioned in the earlier comment, marlin is an A/B device and so I had to change the makeTarget to bootimage.

@samueldr
Copy link
Contributor Author

BTW, I don't have plans to continue working on this branch in the close future.

Looks like my main goal (a cheap way to make a TWRP build for unsupported devices) didn't work on a sample size of 1.

But, as @danielfullmer observed, it's not far from working properly! So if anyone wants to take over and finish the work (in a fresh PR) feel free to do it.

This currently does **not** handle devices.

Fow now you'll have to provide your device tree manually.
Note that while it is deprecated by upstream, it may still be needed for
some device trees that have not been updated to support the android-11
based builds.

This has been verified to correctly build a `sofiar` (Moto g(8)) TWRP
image that boots and works seemingly correctly.
This uses a TWRP device tree generated using

 - https://github.com/SebaUbuntu/TWRP-device-tree-generator

And a known working recovery.img as a base.

Though it does not boot.

I guess one would need to investigate with other devices to have a
better understanding of why.

The recovery.img this produces has the same exact configuration and
files, according to unpackbootimg, except for os level, os version and
cmdline. Re-packing with those from the vendor recovery.img changed
nothing.

So, to recap, I have repacked from an unpackimg extraction BOTH this
TWRP and the stock recovery. The repacked stock recovery still works,
any TWRP will not.

The only difference is the ramdisk.
@samueldr
Copy link
Contributor Author

samueldr commented Dec 4, 2021

I have verified with motorola-rav (Moto g(8)) that this can work.

I'm not sure what should be done about device trees.

The initial PR body has been updated, with a permanent spot for another known good config.

@samueldr

This comment has been minimized.

This ensures overridden symlinks to directories are not attempted to be
linked as a subdir by `ln`.

e.g. with `build/core` already a symlink to something, the following
would not work:

```
   $ mkdir -p build
   $ ln -sf --relative build/make/core build/core
ln: failed to create symbolic link 'build/core/core': Permission denied
```

Observe how the destination is `build/core`, but the link attempted to
be created is `build/core/core`.

From the manpage:

> ```
>     --no-dereference treat LINK_NAME as a normal file if it is a
>                      symbolic link to a directory
> ```
@samueldr
Copy link
Contributor Author

samueldr commented Dec 4, 2021

twrp-9.0 branch verified to still produce a working TWRP for marlin (tested on sailfish).

(surfna produced from twrpdtgen still doesn't work, but eh...)

Comment on lines +42 to +50
twrp = config.build.mkAndroid {
name = "robotnix-${config.productName}-${config.buildNumber}";
makeTargets = [ "recoveryimage" ];
# Note that $ANDROID_PRODUCT_OUT is set by choosecombo above
installPhase = ''
mkdir -p $out
cp --reflink=auto $ANDROID_PRODUCT_OUT/recovery.img $out/
'';
};
Copy link
Contributor Author

@samueldr samueldr Dec 4, 2021

Choose a reason for hiding this comment

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

How should we handle the need to provide either bootimage or recoveryimage, and $ANDROID_PRODUCT_OUT/recovery.img or $ANDROID_PRODUCT_OUT/boot.img conditionally on something I'm not even sure we can get?

Would adding an option be correct?

@samueldr samueldr changed the title WIP TWRP branch twrp: Initial support for TWRP builds Dec 4, 2021
@samueldr samueldr changed the title twrp: Initial support for TWRP builds twrp: Initial basic support for TWRP builds Dec 4, 2021
@Atemu
Copy link
Contributor

Atemu commented May 27, 2022

(Suprisingly) working config for cheeseburger:

# SPDX-FileCopyrightText: 2022 Atemu
# SPDX-FileCopyrightText: 2021 Daniel Fullmer and robotnix contributors
# SPDX-License-Identifier: MIT

#
# Usage
# =====
#
# ```
# $ nix-build --arg configuration ./cheeseburger-twrp.nix -A config.build.twrp
# ```
#

{ pkgs, ... }:

{
  device = "cheeseburger_dumpling";
  androidVersion = 9;
  flavor = "twrp";

  envVars.ALLOW_MISSING_DEPENDENCIES = "true";

  source.dirs = {
    "device/oneplus/cheeseburger_dumpling" = {
      src = pkgs.fetchFromGitHub {
        owner = "TeamWin";
        repo = "android_device_oneplus_cheeseburger_dumpling";
        rev = "61f577680c941ca7c1e601105cad4c21cdc2fac9";
        sha256 = "sha256-i+87wxWAqvnZOtDeiH1+fnvrjqMIedolbKNcS8S5lZ4=";
      };
    };
  };
}

@mannp
Copy link

mannp commented May 31, 2022

Also works for an unsupported device ... in case it helps anyone else.

{ pkgs, ... }:

{
  device = "togari";
  androidVersion = 9;
  flavor = "twrp";

  envVars.ALLOW_MISSING_DEPENDENCIES = "true";

  source.dirs = {
    "device/sony/togari" = {
      src = pkgs.fetchFromGitHub {
        owner = "sony-togari-dev";
        repo = "android_device_sony_togari-twrp";
        rev = "android-9.0";
        sha256 = "sha256-6j3ZRKL+gaBtX2jP0MYr6ktbO6cmqlc9uZmnH2RIct4=";
      };
    };
    "kernel/sony/msm8974" = {
      src = pkgs.fetchFromGitHub {
        owner = "sony-togari-dev";
        repo = "android_kernel_sony_msm8974";
        rev = "lineage-18.1";
        sha256 = "sha256-KXOF7f3B73OjSC6ABiT7EQ6/RzK3DB5ywzEHn8B9P88=";
      };
    };

  };
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants