U-Boot porting

Why bother?
Porting U-boot to your device gives you more control, when booting mainline kernels, namely:
 * 1) Adjusting boot command line
 * 2) Dual-boot without hacks
 * 3) Prevent stock bootloader from kernel / device tree modification

About
This article will guide you on porting u-boot on your phone. It's possible, even if there is no complete SOC support in uboot tree (only uart driver needed), because stock bootloader initialized hardware for us. First thing to focus on should be getting u-boot shell prompt.

Preparation
- bring up process - configuration process
 * 1) Find uboot code
 * 2) Before starting uboot porting, you should get access to the stock bootloader uart port. Make sure you can see stock bootloader logs.
 * 3) Search uboot code for a uart driver, compatible with your SOC. It is good, if it support debug(i.e. include debug_uart.h)
 * 4) Get familiar with you phone RAM map
 * 5) Some useful links:

Read the following readme files:
 * top README
 * doc/README.kconfig
 * doc/usage/environment.rst

Add board files
Use guide from Free Electrons pdf or video version

Examples

 * 1) Add exynos7420 espresso board
 * 2) Add exynos7420 SOC support

Configure
U-boot configuration system comes from linux kernel. Put config options not supposed to be changed by user in  files, in   otherwise

Options, you DO NOT need

 * All SPL related options. SPL(Secondary Program Loader) splits u-boot in two parts. You don't need this, since stock bootloader will load whole u-boot image into RAM.
 * CONFIG_BOARD_EARLY_INIT_F

Options, you need

 * CONFIG_SYS_MALLOC_LEN - size of early C runtime environment heap
 * CONFIG_SYS_INIT_SP_ADDR - address of early C runtime environment stack (should be in RAM address space, but NOT at the bottom, because stack is growing DOWN. see u-boot readme memory management section). This option valid, when.
 * CONFIG_SYS_INIT_SP_BSS_OFFSET - when, defines offset to early c runtime stack from bss section from bss section. Default is 512K.
 * CONFIG_SYS_TEXT_BASE - u-boot base address. It's the address in RAM, where u-boot is loaded by stock bootloader. Not obligatory, if you use position independent build with . See instructions below, how to find it.
 * CONFIG_SYS_SDRAM_BASE - start address of the dynamic RAM. Can be found in memory node in linux device tree.
 * CONFIG_DEBUG_UART=y
 * CONFIG_DEBUG_UART_SKIP_INIT=y - make sure to comment  function code in you serial driver with that option
 * CONFIG_DEBUG_UART_BASE=0x13820000 // your uart address from downstream device tree
 * CONFIG_LOGLEVEL=8 // log all
 * CONFIG_POSITION_INDEPENDENT - Since Galaxy S8, Samsung randomizes kernel load address. You have to enable this option, if  start address is random, i.e. changes each boot. Find   address with
 * CONFIG_LINUX_KERNEL_IMAGE_HEADER - if you want U-boot binary to look as much as Linux kernel image as possible, to pack it inside android boot.img so stock android bootloader can recognize it

Make u-boot
Install arm cross compiler Build: export CROSS_COMPILE=aarch64-linux-gnu- make <$board_name>_defconfig make

Create android boot image
mkbootimg --base 0x40000000 --kernel_offset 0x00008000 --ramdisk_offset 0x01000000 --tags_offset 0x00000100 --pagesize 2048 --second_offset 0x00f00000 --kernel {path to u-boot.bin} -o {output file} Replace offsets to your offsets, found from downstream boot.img.

Running
Flash android image, or follow Bootloaders_porting_using_linux guide

Raw
This may be useful, when no C runtime environment set up yet. You should find uart address to write

C
*(volatile unsigned char *)(0x13820020/*uart register to write characters*/) = 0x48; // print H letter

Assembly
mov x26, #0x20 // move 0x13820020 low 16 bit to x26 register movk x26, #0x1382, lsl #16 // move 0x13820020 high 16 bit to x26 register mov x27, #0x48 // H letter ascii code str x27, [x26] // move H to uart register

Using debug function
You can get debug output from particular *.c file by adding  at the top of file, and calling   where you need.

Finding base address
Unless you build u-boot with, you should tell u-boot base address, i.e. address in RAM, where stock bootloader load u-boot. This section describes, how to find it.

Without the knowledge of base address u-boot will fail on  file when calling functions from. This is a list of initcall addresses, and if CONFIG_SYS_TEXT_BASE does not match with address u-boot resides, you CPU will jump in wrong address and execute irrelevant code, leaving you with unpredictable uart output and results.

will output something like:

address range start will be your  address.

Creating basic device tree
Will contain node for uart driver, fixed clock and chosen.

a5y17lte device tree example
exynos7880.dtsi: // SPDX-License-Identifier: GPL-2.0+ /dts-v1/; / { 	compatible = "samsung,exynos7880"; fin_pll: xxti { compatible = "fixed-clock"; clock-output-names = "fin_pll"; u-boot,dm-pre-reloc; #clock-cells = <0>; }; 	uart2: serial@13820000 { compatible = "samsung,exynos4210-uart"; reg = ; u-boot,dm-pre-reloc; }; };
 * 1) include "skeleton.dtsi"

a5y17lte.dts: // SPDX-License-Identifier: GPL-2.0+ /dts-v1/; / { 	compatible = "samsung,exynos7880"; aliases { console = &uart2; };    chosen { stdout-path = &uart2; }; }; &fin_pll { clock-frequency = ; };
 * 1) include "exynos7880.dtsi"

Final changes
You may need to comment some serial driver initialization code, as it may not work properly without clock and pinctrl drivers.

Testing future u-boot builds
Once you entered in u-boot console, you may upload new u-boot build in RAM via uart and run it

Upload a file into RAM

 * on u-boot console: . If no address specified, file will be loaded at CONFIG_SYS_LOAD_ADDR
 * exit u-boot console to release tty device

With ckermit

 * install ckermit (build it yourself with `-DSCO_OSR504` flag, for 115200+ baudrates)
 * create a script to send file:

set port /dev/ttyUSB0 set speed 921600 set carrier-watch off set flow-control none set prefixing all send \%1 exit
 * 1) !/bin/ckermit +

Source

Run uboot test version
On u-boot console:

Hush shell
There is scripting possibility in u-boot with hush shell.


 * Build u-boot with CONFIG_HUSH_PARSER.
 * Write simple  file like (Note, that empty line in you file will repeat previous command!)

for i in a b c; do    echo $i done


 * Make uboot image
 * Upload file to RAM as described above
 * Run
 * - check and print script info
 * - run script

Using initramfs in boot partition
This method is simple, and doesn't require storage driver in secondary bootloader.

Requirements:
 * device with u-boot support as secondary bootloader
 * secondary bootloader supports at least one button (for the boot variant selection)

It works by creating and flashing android boot image with your secondary bootloader replacing linux kernel, and secondary bootloader payload replacing initramfs. Multiboot implemented by having multiple payloads in initramfs replacement, and a script to pick one. Boot sequence in this case would be as follows: - stock BL -> u-boot(replaces linux)->|-> linux |              ^          |-> initramfs |              |          |-> dtb |-> u-boot's payload(replaces initramfs)

Android booting has some peculiarities:
 * we should use dtb, loaded by stock BL(because it usually adjusts it)
 * repacking only kernel and initramfs from stock android boot image into u-boot payload

Boot sequence in such case would be:

stock BL -> u-boot(replaces linux)->|-> linux---|-> Android |              ^          |-> initramfs--/| |              |                                  |          |-> u-boot's payload(replaces initramfs)           | |                                                 |         |-> android's DTB-/

Related merge request: boot-deploy

Implement multiboot involving Android in a device aport
See starqltechn example


 * check device meets requirement in
 * create source files:
 * create FIT image source file for Android
 * create  script to find all images in memory, and select needed. Create FIT source file for it.
 * add  file to   option. boot-deploy script will build all provided files, and put it together in initramfs replacement file
 * aports: enable deviceinfo options; see also:
 * deviceinfo_bootimg_vendor_dependent
 * deviceinfo_bootimg_vendor_android_boot_image
 * deviceinfo_bootimg_vendor_device_tree_identifiers
 * rerun . It will prompt for vendor boot image location.
 * run.
 * Backup boot partition
 * Flash new image

Suppose, you wrote  so that it boots Android, when volume down button pressed. To boot Android, press power on, wait for vendor logo, then press volume down.

Related merge requests:
 * boot-deploy MR
 * mkinitfs MR
 * pmbootstrap

Failed to reserve memory...
You may need to increase LMB_MAX_REGIONS config to be higher, than reserved memory regions count in your linux dts. This is because u-boot tries to reserved all reserved memory regions, and it should have regions in lmb library available.