How to Boot an Android Emulator Image Using U-Boot
Do you have difficulties with debugging U-Boot on an Android device in your project? Are you wondering how to start the Android system from scratch? These are common puzzles in the areas of Android system programming. To explore embedded system programming or Android system programming, you will need a hardware board, but the hardware might not always be available. How can you get started with something you already have on hand? The Android emulator is actually a good choice, even for system-level programming. In this article, I address how to use U-Boot to boot an Android emulator image.
U-Boot is a popular bootloader used in many embedded applications. Android system developers often use it to boot various Android-based embedded systems. As an Android application developer, you might use it to enter recovery mode or to restore the factory default settings for your testing devices. However, no bootloader is available in the Android emulator environment, which Android application developers use most of the time. Let's take a look at the details of how to use U-Boot in the Android emulator environment. Using U-Boot, Android system developers and application developers can gain the flexibility to manipulate the Android emulator system image. For example, testing and debugging issues related to recovery mode is almost impossible in the emulator environment. With a bootloader, you shouldn't have any problems testing recovery mode in the emulator environment.
This article will not discuss on how to port U-Boot to the Android emulator or how to build it for the Android emulator. Instead, we'll focus on how to use the U-Boot available in my book Embedded Programming with Android: Bringing Up an Android System from Scratch to design different usage scenarios for the Android emulator. The information in this article will help Android system developers and application developers to work with test cases that need a bootloader in the Android emulator environment.
How to Get the U-Boot for Android Emulator
The U-Boot for Android emulator can be downloaded from SourceForge, a web-based service that offers a source code repository, download mirrors, bug tracking, and other features. Two binary packages contain the U-Boot image for the Android emulator:
Both packages are built for KitKat (Android version 4.4.4 and API level 19). To test it, you need to download the Android software development kit (SDK) and install Android emulator images for API level 19. You can test these two packages using your Android SDK installation, which means you can test them in Windows, Linux, or OS X environments. This article demonstrates the setup in Ubuntu Linux 12.04.
After downloading these two packages, put them in a folder called images under your home folder:
$ ls images aosp-bin.zip cm-bin.zip
Next, you need to decompress the packages. After decompression, check the contents inside the packages:
$ ls -F images aosp-bin/ aosp-bin.zip cm-bin/ cm-bin.zip $ ls images/aosp-bin aosp-test.bat ramdisk.img system.img userdata.img aosp-test.sh Readme.txt u-boot.bin $ ls images/cm-bin cm-test.bat ramdisk.img system.img userdata.img cm-test.sh Readme.txt u-boot.bin
Scripts for testing
You can use scripts aosp-test.bat/cm-test.bat to test AOSP or CyanogenMod in Windows, or aosp-bin.sh/cm-test.sh to test in Linux or OS X.
This is the U-Boot image for the Android emulator. Although this image can be used for any Android version, it has to build for a particular Android emulator version. It has no dependency to the upper layer (operating system and applications above it), but it has to build for the virtual hardware platform, called goldfish, which is the code name of the Android emulator virtual hardware platform. The virtual hardware interface of the Android emulator may differ in various Android SDK releases. The U-Boot image in this example is built for the Android SDK adt-bundle-linux-x86-20140702, but it should also work for the latest Android SDK.
This is a version of the Android emulator system image built using AOSP version android-4.4.4_r2 or CyanogenMod version cm-11.
This is a version of the Android emulator data image built using AOSP version android-4.4.4_r2 or CyanogenMod version cm-11.
This is a version of the Android emulator RAM disk image built using AOSP version android-4.4.4_r2 or CyanogenMod version cm-11.
This file explains how to use the packages.
Setting Up the Android Virtual Device to Test AOSP and CyanogenMod Emulator Images
Before testing using U-Boot to boot the Android emulator image, you have to set up an Android Virtual Device (AVD). We'll use the Android Virtual Device Manager for this purpose.
You can launch the Android Virtual Device Manager from the Eclipse IDE, Android Studio, or the command line. In order to use the script in the downloaded package, we'll need to create two Android Virtual Devices, named armemu for AOSP and cm_armemu for CyanogenMod. Use the following command:
$ android avd
Once you've launched the Android Virtual Device Manager, create an Android Virtual Device as shown in Figure 1.
Figure 1 Setting up an Android Virtual Device.
In Figure 1, an Android Virtual Device named armemu for Android 4.4.2, API Level 19 was created. The target CPU/ABI is armeabi-v7a because the U-Boot image used here is for the ARM processor architecture. I choose 512MB RAM and 200MB internal storage space for the settings. The hardware spec for cm_armemu is the same as in Figure 1 except for the device name.
After setting up the Android Virtual Devices armemu and cm_armemu, we can boot up the Android emulator images using U-Boot. Let's test the AOSP build first, using these commands:
$ cd aosp-bin $ ./aosp-test.sh
Enter the aosp-bin folder and launch the Android emulator image, using the script aosp-test.sh. This script executes the following command:
$ emulator -avd armemu -verbose -show-kernel -shell -system ./system.img -initdata ./userdata.img -ramdisk ./ramdisk.img -qemu -kernel ./u-boot.bin
This command starts the Android emulator with AVD name armemu and specifies the system.img, userdata.img and ramdisk.img files. It uses U-Boot image u-boot.bin as the kernel image to start the emulator.
Once the emulator enters the Android home screen, we can check the software version information from the About phone screen inside Settings, as shown in Figure 2.
Figure 2 Android version of the AOSP image.
We can do the same for the CyanogenMod image. Launch the Android emulator image for CyanogenMod by using the following commands:
$ cd ../cm-bin $ ./cm-test.sh
Once the emulator enters the Android home screen, check the software version information in the same way as we did for the AOSP version. Notice the version information shown in Figure 3.
Figure 3 Android version of CyanogenMod image.
After using U-Boot to set up and test the Android emulator images for AOSP or CyanogenMod, we can refer to the boot log for more analysis. Using the boot log from the AOSP image, for example, shows the analysis below:
U-Boot 2013.01.-rc1-00004-g7d47698 (Apr 13 2015 - 09:49:09) U-Boot code: 00010000 -> 00070F24 BSS: -> 000A9210 IRQ Stack: 0badc0de FIQ Stack: 0badc0de monitor len: 00099210 ramsize: 20000000 TLB table at: 1fff0000 Top of RAM usable for U-Boot at: 1fff0000 Reserving 612k for U-Boot at: 1ff56000 Reserving 4104k for malloc() at: 1fb54000 Reserving 32 Bytes for Board Info at: 1fb53fe0 Reserving 120 Bytes for Global Data at: 1fb53f68 Reserving 8192 Bytes for IRQ stack at: 1fb51f68 New Stack Pointer is: 1fb51f58 RAM Configuration: Bank #0: 00000000 512 MiB relocation Offset is: 1ff46000 goldfish_init(), gtty.base=ff012000 WARNING: Caches not enabled monitor flash len: 00068C4C Now running in RAM - U-Boot at: 1ff56000 NAND: base=ff017000 goldfish_nand_init: id=0: name=nand0, nand_name=system goldfish_nand_init: id=1: name=nand1, nand_name=userdata goldfish_nand_init: id=2: name=nand2, nand_name=cache 517 MiB MMC: board_mmc_init called goldfish_mmc: 0 Using default environment Destroy Hash Table: 1ffb5be8 table = 00000000 Create Hash Table: N=104 INSERT: table 1ffb5be8, filled 1/107 rv 1fb542b0 ==> name="bootargs" value="qemu.gles=0 qemu=1 console=ttyS0 android.qemud=ttyS1 androidboot.console=ttyS2 android.checkjni=1 ndns=1" INSERT: table 1ffb5be8, filled 2/107 rv 1fb54394 ==> name="bootcmd" value="ydevconfig sys 0 0x0 0x7f0 ; ymount sys ; yrdm sys/ramdisk.uimg 0x610000 ; yrdm sys/zImage.uimg 0x210000 ; yumount sys ; bootm 0x210000 0x610000" INSERT: table 1ffb5be8, filled 3/107 rv 1fb5440c ==> name="bootdelay" value="2" INSERT: table 1ffb5be8, filled 4/107 rv 1fb54478 ==> name="baudrate" value="38400" INSERT: table 1ffb5be8, filled 5/107 rv 1fb54604 ==> name="bootfile" value="/tftpboot/uImage" INSERT: free(data = 1fb54008) INSERT: done In: serial Out: serial Err: serial Net: SMC91111-0 Warning: SMC91111-0 using MAC address from net device ### main_loop entered: bootdelay=2 ### main_loop: bootcmd="ydevconfig sys 0 0x0 0x7f0 ; ymount sys ; yrdm sys/ramdisk.uimg 0x610000 ; yrdm sys/zImage.uimg 0x210000 ; yumount sys ; bootm 0x210000 0x610000" Hit any key to stop autoboot: 0 Configures yaffs mount sys: dev 0 start block 0, end block 2032 Mounting yaffs2 mount point sys Copy sys/ramdisk.uimg to 0x00610000... [DONE] Copy sys/zImage.uimg to 0x00210000... [DONE] Unmounting yaffs2 mount point sys ## Current stack ends at 0x1fb51b00 * kernel: cmdline image address = 0x00210000 ## Booting kernel from Legacy Image at 00210000 ... Image Name: Created: 2015-04-13 1:49:27 UTC Image Type: ARM Linux Kernel Image (uncompressed) Data Size: 2423552 Bytes = 2.3 MiB Load Address: 00010000 Entry Point: 00010000 kernel data at 0x00210040, len = 0x0024fb00 (2423552) * ramdisk: cmdline image address = 0x00610000 ## Loading init Ramdisk from Legacy Image at 00610000 ... Image Name: Created: 2015-04-13 1:49:27 UTC Image Type: ARM Linux RAMDisk Image (uncompressed) Data Size: 323724 Bytes = 316.1 KiB Load Address: 00800000 Entry Point: 00800000 ramdisk start = 0x00800000, ramdisk end = 0x0084f08c Loading Kernel Image ... OK OK kernel loaded at 0x00010000, end = 0x0025fb00 images.os.start = 0x210000, images.os.end = 0x45fb40 images.os.load = 0x10000, load_end = 0x25fb00 using: ATAGS ## Transferring control to Linux (at address 00010000)... Starting kernel ... Uncompressing Linux... done, booting the kernel.
From this boot log, we can see that after U-Boot starts, it prints the following version information:
U-Boot 2013.01.-rc1-00004-g7d47698 (Apr 13 2015 - 09:49:09)
This U-Boot image was built on April 13, 2015 and is based on version 2013.01.-rc1-00004-g7d47698 build from the Linaro release.
During startup, this image initializes the following NAND devices:
goldfish_nand_init: id=0: name=nand0, nand_name=system goldfish_nand_init: id=1: name=nand1, nand_name=userdata goldfish_nand_init: id=2: name=nand2, nand_name=cache
After U-Boot initializes the hardware interfaces, the following boot commands are used to boot the Android system:
bootcmd="ydevconfig sys 0 0x0 0x7f0 ; ymount sys ; yrdm sys/ramdisk.uimg 0x610000 ; yrdm sys/zImage.uimg 0x210000 ; yumount sys ; bootm 0x210000 0x610000"
Let's look into the details and see how to use these commands to boot the Android system. First, we use U-Boot command ydevconfig to configure the NAND device:
ydevconfig sys 0 0x0 0x7f0
The first parameter (sys) is the name that we choose for device nand0. The second parameter is the number of the NAND device. (Device ID 0 is the NAND device storing the Android file system.) The third parameter is the starting block, and the fourth parameter is the end block.
Once the NAND device is configured, we use the ymount command to mount the NAND device:
Thereafter, we can use the yrdm command to load the RAM disk image and kernel image to memory:
yrdm sys/ramdisk.uimg 0x610000 yrdm sys/zImage.uimg 0x210000
Once the kernel image and RAM disk image are loaded into memory, we can see from the above boot log that the kernel image is loaded at memory address 0x210000 and the RAM disk image is at memory address 0x610000.
Now we can unmount the NAND device, using U-Boot command yumount, and boot to the operating system using the bootm command:
yumount sys bootm 0x210000 0x610000
How to Get the U-Boot Console
In the files aosp-bin.zip or cm-bin.zip, the U-Boot image is built with proper boot commands so that U-Boot can boot operating system automatically. It is difficult to get the U-Boot command console using U-Boot in these two packages. With U-Boot command console, we can debug the boot process and do something extra. An U-Boot image which can boot to U-Boot command console is posted at u-boot_goldfish.zip. You can download and do your own testing with it. After you download u-boot_goldfish.zip and decompress it, you'll see two files:
$ cd u-boot_goldfish $ ls Readme.txt u-boot.bin
You can get the U-Boot console by using the following commands:
$ emulator -avd armemu -show-kernel -shell -qemu -kernel ./u-boot.bin ... ### main_loop entered: bootdelay=2 ### main_loop: bootcmd="ver" Hit any key to stop autoboot: 0 U-Boot 2013.01.-rc1-00004-g7d47698-dirty (Jul 19 2015 - 19:53:01) arm-none-eabi-gcc (Sourcery CodeBench Lite 2012.03-56) 4.6.3 GNU ld (Sourcery CodeBench Lite 2012.03-56) 126.96.36.19910905 armemu #
Once you enter the U-Boot command console, you can test various U-Boot commands available in the U-Boot manual. For example, you can get version information and check environment variables as shown below:
armemu # ver U-Boot 2013.01.-rc1-00004-g7d47698-dirty (Jul 19 2015 - 19:53:01) arm-none-eabi-gcc (Sourcery CodeBench Lite 2012.03-56) 4.6.3 GNU ld (Sourcery CodeBench Lite 2012.03-56) 188.8.131.5210905 armemu # printenv EXPORT table = 1ffb601c, htab.size = 89, htab.filled = 11, size = 0 Unsorted: n=11 0: 1fb54144 ==> bootfile => /tftpboot/uImage 1: 1fb54150 ==> bootcmd => ver 2: 1fb5415c ==> stderr => serial 3: 1fb54168 ==> baudrate => 38400 4: 1fb54294 ==> bootargs => qemu.gles=0 qemu=1 console=ttyS0 android.qemud=ttyS1 androidboot.console=ttyS2 android.checkjni=1 ndns=1 5: 1fb542b8 ==> stdin => serial 6: 1fb542e8 ==> bootdelay => 2 7: 1fb543c0 ==> ethaddr => 52:54:00:12:34:56 8: 1fb54414 ==> verify => n 9: 1fb544bc ==> stdout => serial 10: 1fb544ec ==> ethact => SMC91111-0 baudrate=38400 bootargs=qemu.gles=0 qemu=1 console=ttyS0 android.qemud=ttyS1 androidboot.console=ttyS2 android.checkjni=1 ndns=1 bootcmd=ver bootdelay=2 bootfile=/tftpboot/uImage ethact=SMC91111-0 ethaddr=52:54:00:12:34:56 stderr=serial stdin=serial stdout=serial verify=n Environment size: 296/8188 bytes armemu #
As you can see from the environment variables above, for this build of U-Boot, the variable bootcmd was set as ver. After U-Boot is loaded, it will just print version information and enter the command console.
What Else Can You Do with U-Boot?
With U-Boot support, you can explore various methods to boot the Android system emulator. For example, you can test the boot process from either NOR or NAND flash.
Without a bootloader, you can't test cases related to recovery mode. With U-Boot, you can build your own recovery images and test them using U-Boot. Furthermore, you can test multi-boot systems, just like you can on a PC. You can put more than one Android system into one system image and choose to boot into any of them by using U-Boot.
In summary, with proper bootloader support, you can make your emulator environment closer to the real hardware environment. From there, you can explore things that are impossible without a bootloader in the emulator environment.