Skip to content

Instantly share code, notes, and snippets.

@skinzor
Forked from mvaisakh/Bringup.md
Created January 29, 2023 13:07
Show Gist options
  • Save skinzor/89e0c11637830d7d6129bc3b50dd87c7 to your computer and use it in GitHub Desktop.
Save skinzor/89e0c11637830d7d6129bc3b50dd87c7 to your computer and use it in GitHub Desktop.
An Android Device Tree Bringup Guide

A small Device Tree Bringup Guide

Introduction

So, you guys might be wondering, how do these "Developers" get your favourite Custom roms, such as LineageOS, Paranoid Android etc., to their own devices. Well I'm here to Guide you on how to do it, specifically on how to bringup or make your own device tree from scratch or adapting.

Gist of this Guide: This is for people with genuine interest in Android OS porting/development. After going through this guide, you should be able to do a total device tree bringup on your own.

Prerequisite: Certain requirements are to be met before you start with this amazing journey.

  • A brain (The most important part)

  • A computer/server powerful enough to build android roms (Out of the scope of this guide, though you should check out minimum specifications required to build android from AOSP)

  • Basic text editing.

  • Git knowledge

  • A way around Android Build system

  • Familiarity with Linux/MacOS Command Line Interface

  • Note: I assume that you already know about Linux directories and Android Build System (How Android.mk works, etc.), which to be explained is out of the scope of this guide.

  • Note 2: This guide applies to devices that were launched with Project Treble enabled (Android Oreo 8.0+), devices launched without it, are not supported on this thread.

  • Note 3: I'd be specifically talking about a Qualcomm Device (Snapdragon SDM660 based device, launched with Android Oreo 8.1.0) as a reference on this guide. I'm really not experienced with MediaTek or other SoCs device trees , if anyone knows about them, feel free to add a reply to this thread.

  • Note 4: This guide uses Paranoid Android as the Android Rom Build System. Each and every rom has a different/unique base. This rom here is based on CodeAuroraForum (CAF) Collaborative Project.

The Guide

For refreshing, android device trees are certain bits of makefile code (also has certain other parts), that allows the Android Build system to recognise the target device specifications to build for, and what all to be built with it.

According to the Google's conventions, a device is usually placed in a specific location:

device/<OEM Name or Vendor Name>/<Device Codename>/

There's a script located in device/common to populate the basic makefiles according to the format I specified above. But, it's better to do it manually to have a better understanding and learning on how to do it. Understanding Basic Structure of the device tree

When we look at certain device trees, there are certain files and folders we come across, as you can see here

Device Tree Structure

Let's break these down:

  • Android.mk: This is the most basic makefile definition for any path to be included in the android build system. This makefiles calls for all other makefiles present in the directory and subdirectories of the device tree. The basic use of this specific makefile is to guard the device tree makefile when not building for our target device.
  • BoardConfig.mk: This contains the Board (SoC/Chipset) specific definition, such as the architecture (arm, arm64, x86, etc.), required build flags and kernel build flags. The flags in these aid during building.
  • device.mk: This makefile contains all the build targets and copy specifications (Such as audio configs, feature configs, etc.). If you want a certain package to be built inline with android rom builds, this where you'd want to define it. The vendor tree makefiles are also called from here.
  • README.md: Contains the basic info about the device. It's always good to have documentation.
  • config.fs: Read more about it on AOSP.
  • extract-files.sh, setup-makefiles.sh & proprietary-files.txt: These files are linked to each other. These are used to extract proprietary blobs from a device and/or from a stock rom dump. Proprietary-files.txt is a list of proprietary (Non-opensource or blobs with proprietary OEM modifications) binaries, which is to be extracted and pushed to vendor tree. Read more about it from LineageOS Wiki.
  • props.mk or any .prop: These contain the specifications needed for build.prop or commonly known as runtime properties.
  • framework-manifest.xml: These are HIDL manifest required by the system framework features and HAL features to work. Read more about them at AOSP.
  • rootdir directory: This contains the major init scripts that are executed during boot time. These are usually found in etc/ dir of /system and /vendor partition.
  • sepolicy directory: This folder contains the specific SELinux policies that are required for the Mandatory Access Control (MAC). Read more about it on AOSP.
  • overlay: This is an important directory. This contains all the framework additions and removals (Since the android framework is largely based on Java, Kotlin and XML, framework configs are controlled from xmls in overlays rather than flags in BoardConfig.mk).
  • aosp_device-name.mk: This is a common makefile found in most AOSP trees. This specific makefile defines device name and manufacturer and build product details. This makefile for my device can be found in Paranoid Android vendor repo (As I mentioned earlier, every rom has their base, which can also mean they have their own way of including device trees)
  • AndroidProducts.mk: This makefile includes the device specific makefiles, i.e., the above mentioned aosp_device codename.mk on lunching (term used to initialise device trees for build). This makefile also defines the lunch target (Android 10+).

Now that we know what the Android Device tree are made up of, let's move on ahead with actually getting our device tree up!

You'll need to figure out the codename of the device by looking at the build number or reading the build.prop or checking out the stock rom dump.

Since I have a qualcomm device, I started digging into CAF OSS board trees, they have certain codenames for their boards (sdm660 is called falcon), you should be able to find them by a google search. If you can't find your Board OSS tree, there's a high chance that it uses a major board type for your specific board (eg., msm8937 uses thorium trees, and since then, sdm450 etc., have used thorium as their base tree). You should take a look at the Board trees present here.

Now my tree here is falcon_64 (64 suffix is here because it's an arm64 device, your board may or may not have it). Open the OSS Board Tree git repository, and use the tag/branch that matches your android version that you want to bring up. note: CAF tags starting with LA.UM.7.x are Android 9.0 pie, LA.UM.8.x are Android 10, and so on.

Now what we need to do is, create the above mentioned files in the device tree. (It's better to initialise a repository beforehand)

Starting with Android.mk, we'd need to add the guard conditions (using ifeq) that should be like:

ifeq ($(TARGET_DEVICE),<Your Device Codename>)
endif

This means that only if the TARGET_DEVICE variable is set to your device, it will start including this device tree, else this tree would be ignored.

My device's codename here is X01BD

You should also add an inclusion command so that all other makefiles in the sub directory are included, such as:

ifeq ($(TARGET_DEVICE),X01BD)
 
  subdir_makefiles=$(call first-makefiles-under,$(LOCAL_PATH))
  $(foreach mk,$(subdir_makefiles),$(info including $(mk) ...)$(eval include $(mk)))
 
endif

Basic Android.mk structure is done!

Moving on to BoardConfig.mk, we need to add architecture specifications, this is where the OSS Board Tree comes in handy, as you can just take all the flags from there. So, almost all the BoardConfig flags are written in a specific format, i.e.,

# Every flag is capitalised
TARGET_ARCH
# Every flag is initialised with ":="
TARGET_ARCH := arm64
# You can have additional values to the same flag by using the append symbol ("+=") instead of initialise symbol (":=")
BOARD_KERNEL_CMDLINE := #(This is initialised)
BOARD_KERNEL_CMDLINE += androidboot.selinux=permissive #(This is appending and adding more info to the existing flag).

Note: The '#' here is a comment.

Copy most of the flags from the Board OSS tree, for reference use my BoardConfig to check what to copy and what not to copy. Most of the flags should be similar. Note: Kernel flags may or may not be same across different boards. Make sure you get the proper ones. And additional flag that you might want to add is

BOARD_KERNEL_CMDLINE += androidboot.selinux=permissive

This is to allow the selinux to be permissive and help us boot avoiding selinux shenanigans. You should also look into other .mk files for specific flags in the Board OSS tree.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment