	Xvisor on Raspberry Pi2 board (or BCM2836) using SD Booting 

This section of readme explains how to build & run Xvisor on actual BCM2836
SOC boards (Raspberry Pi2 for example) using SD booting.

By default, Xvisor for BCM2836 supports Vexpress-A15 guest so we will show
how to run Basic Firmware on Vexpress-A15 guest. For more info refer,
<xvisor_source>/tests/arm32/vexpress-a15/basic/README.

The Raspberry Pi 2 usually comes with and SD card containig the GPU firmware
necessary to boot the ARM processor.
To create your own SD Card to boot the Raspberry Pi 2
 1) Download and write prebuild image to SD card as described in
 https://www.raspberrypi.org/documentation/installation/installing-images/
 2) Optionally download the following files from the Raspberry Official
 Repository (https://github.com/raspberrypi/firmware/tree/master/boot)
 to the SD card if you are not able detect complete 1GB RAM:
 - fixup.dat
 - fixup_cd.dat
 - start.elf
 - start_cd.elf
 (Note: the _cd files are used when gpu_mem=16)

As first step, we install u-boot-2015.07 on the SD card that comes with
Raspberry Pi2 board or that has been formatted as instructed above.
To do this:
 1) Download and build u-boot-2015.07
 # wget ftp://ftp.denx.de/pub/u-boot/u-boot-2015.07.tar.bz2
 # tar -xvf u-boot-2015.07.tar.bz2
 # cd u-boot-2015.07
 # make rpi_2_defconfig
 # make all
 # cat <xvisor_source>/docs/arm/bcm2836-raspi2-bootblk.bin.ift u-boot.bin > u-boot.hyp
 2) Mount the boot partition of pre-installed SD card
 3) Copy u-boot.hyp to <raspi2_sd_card_boot_partition>
 4) Create <raspi2_sd_card_boot_partition>/config.txt if it does not exist
 5) Add following lines to <raspi2_sd_card_boot_partition>/config.txt:
 kernel=u-boot.hyp
 kernel_old=1
 gpu_mem=64
 (Note: if <raspi2_sd_card_boot_partition>/config.txt does not exist then
  create it)
 6) Unmount the boot partition of pre-installed SD card

Once we have u-boot installed on SD card, we add 3 more images to the boot
partition of SD card:
 1) U-Boot compatible Xvisor binary (uvmm.bin)
 2) DTB for Xvisor (one_guest_vexpress-a15.dtb)
 3) Disk image containing guest binaries (disk.img)

Following are steps to create these images and boot Xvisor on Raspberry Pi2:

  [1. Build environment]
  # export CROSS_COMPILE=arm-linux-gnueabi-

  [2. GoTo Xvisor source directory]
  # cd <xvisor_source_directory>

  [3. Configure Xvisor with Generic-v7-ve default settings]
  # make ARCH=arm generic-v7-ve-defconfig

  [4. Build Xvisor and DTBs]
  # make; make dtbs

  [5. Make U-Boot compatible uvmm.bin for use with bootm command]
  # mkimage -A arm -O linux -T kernel -C none -a 0x00008000 -e 0x00008000 -n Xvisor -d build/vmm.bin build/uvmm.bin

  [6. Build Basic Firmware]
  # make -C tests/arm32/vexpress-a15/basic

  [7. Copy defconfig to Linux build directory]
  # cp tests/arm32/vexpress-a15/linux/linux-<linux_version>_defconfig <linux_build_directory>/.config

  [8. GoTo Linux source directory]
  # cd <linux_source_directory>

  [9. Configure Linux in build directory]
  # make O=<linux_build_directory> ARCH=arm oldconfig

  [10. Build Linux in build directory]
  # make O=<linux_build_directory> ARCH=arm Image

  [11. Create BusyBox RAMDISK to be used as RootFS for Linux kernel]
  (Note: For subsequent steps, we will assume that your RAMDISK is located at <busybox_rootfs_directory>/rootfs.img)
  (Note: Please refer tests/common/busybox/README.md for creating rootfs.img using BusyBox)

  [12. GoTo Xvisor source directory]
  # cd <xvisor_source_directory>

  [13. Create disk image for Xvisor with Guest Linux and Guest Basic Firmware]
  # mkdir -p ./build/disk/tmp
  # mkdir -p ./build/disk/system
  # cp -f ./docs/banner/roman.txt ./build/disk/system/banner.txt
  # cp -f ./docs/logo/xvisor_logo_name.ppm ./build/disk/system/logo.ppm
  # mkdir -p ./build/disk/images/arm32/vexpress-a15
  # ./build/tools/dtc/dtc -I dts -O dtb -o ./build/disk/images/arm32/vexpress-a15x2.dtb ./tests/arm32/vexpress-a15/vexpress-a15x2.dts
  # cp -f ./build/tests/arm32/vexpress-a15/basic/firmware.bin ./build/disk/images/arm32/vexpress-a15/firmware.bin
  # cp -f ./tests/arm32/vexpress-a15/linux/nor_flash.list ./build/disk/images/arm32/vexpress-a15/nor_flash.list
  # cp -f ./tests/arm32/vexpress-a15/linux/cmdlist ./build/disk/images/arm32/vexpress-a15/cmdlist
  # cp -f <linux_build_directory>/arch/arm/boot/Image ./build/disk/images/arm32/vexpress-a15/Image
  # ./build/tools/dtc/dtc -I dts -O dtb -p 0x800 -o ./build/disk/images/arm32/vexpress-a15/vexpress-v2p-ca15-tc1.dtb ./tests/arm32/vexpress-a15/linux/vexpress-v2p-ca15-tc1.dts
  # cp -f <busybox_rootfs_directory>/rootfs.img ./build/disk/images/arm32/rootfs.img
  # genext2fs -B 1024 -b 16384 -d ./build/disk ./build/disk.img
  OR
  [13. Create disk image for Xvisor with only Guest Basic Firmware]
  # mkdir -p ./build/disk/tmp
  # mkdir -p ./build/disk/system
  # cp -f ./docs/banner/roman.txt ./build/disk/system/banner.txt
  # cp -f ./docs/logo/xvisor_logo_name.ppm ./build/disk/system/logo.ppm
  # mkdir -p ./build/disk/images/arm32/vexpress-a15
  # ./build/tools/dtc/dtc -I dts -O dtb -o ./build/disk/images/arm32/vexpress-a15x2.dtb ./tests/arm32/vexpress-a15/vexpress-a15x2.dts
  # cp -f ./build/tests/arm32/vexpress-a15/basic/firmware.bin ./build/disk/images/arm32/vexpress-a15/firmware.bin
  # cp -f ./tests/arm32/vexpress-a15/basic/nor_flash.list ./build/disk/images/arm32/vexpress-a15/nor_flash.list
  # genext2fs -B 1024 -b 16384 -d ./build/disk ./build/disk.img

  [14. Make U-boot compatible initrd for use with bootm command]
  # mkimage -A arm -O linux -T ramdisk -a 0x00000000 -n "Xvisor ramdisk" -d build/disk.img build/udisk.img

  [15. Unplug bootable SD card from the Raspberry Pi2 board and plug it on your development machine]

  [16. Mount data partition (or partition 0) of SD card on your development machine]

  [17. Copy uvmm.bin, DTB, and disk.img to data partition of SD card]
  # cp -f build/uvmm.bin <path_to_mounted_data_partition_of_sd_card>
  # cp -f build/arch/arm/board/generic/dts/bcm2836/one_guest_vexpress-a15.dtb <path_to_mounted_data_partition_of_sd_card>
  # cp -f build/udisk.img <path_to_mounted_data_partition_of_sd_card>

  [18. Unmount data partition (or partition 0) of SD card from your development machine]

  [19. Unplug SD card from your development machine and plug it back on Raspberry Pi2 board]

  [20. Connect to serial port of Raspberry Pi2 board using Putty or Minicom]

  [21. Power-up or Reset Raspberry Pi2 board and press any key when U-Boot shows auto-boot count down]

  [22. Enable MMC interface from U-Boot command line]
  U-Boot> mmc dev 0:0

  [23. Copy Xvisor from SD card to SDRAM]
  U-Boot> ext4load mmc 0:2 0x200000 uvmm.bin

  [24. Copy the Xvisor DTB from SD card to SDRAM]
  U-Boot> ext4load mmc 0:2 0x800000 one_guest_vexpress-a15.dtb

  [25. Copy disk image from SD card to SDRAM]
  U-Boot> ext4load mmc 0:2 0x2000000 udisk.img

  [26. Jump into Xvisor after disabling MMU, interrupts, etc]
  U-Boot> bootm 0x200000 0x2000000 0x800000

  (Note: If you want to "autoboot" then you need to create a boot.scr file
   on the root of the SD card. To do so edit a boot.cmd text file and add all
   the commmands you want to execute automatically. For example you can put
   the following commands in the "boot.cmd" file:
   mmc dev 0:0
   fatload mmc 0 0x200000 uvmm.bin
   fatload mmc 0 0x800000 one_guest_vexpress-a15.dtb
   fatload mmc 0 0x2000000 udisk.img
   bootm 0x200000 0x2000000 0x800000
   Then you can create the boot.scr file wit the following command:
   # mkimage -C none -A arm -T script -d boot.cmd boot.scr
   Put the boot.scr file a the root of your SD card.)

  [27. Kick Guest0 for starting Basic Firmware]
  XVisor# guest kick guest0

  [28. Bind to virtual UART]
  XVisor# vserial bind guest0/uart0

  [29. Try few commands of Basic firmware or goto next step]
  [guest0/uart0] basic# hi
  [guest0/uart0] basic# hello
  [guest0/uart0] basic# help

  [30. Copy linux from NOR flash to RAM and start linux booting from RAM]
  [guest0/uart0] basic# autoexec
  (Note: "autoexec" is a short-cut command)
  (Note: The <xvisor_source_directory>/tests/arm32/vexpress-a15/linux/cmdlist
   file which we have added to guest NOR flash contains set of commands for booting
   linux from NOR flash)

  [31. Wait for Linux prompt to come-up and then try out some commands]
  [guest0/uart0] / # ls

  [32. Enter character seqence 'ESCAPE+x+q" return to Xvisor prompt]
  [guest0/uart0] / #

  (Note: replace all <> brackets based on your workspace)
  (Note: some of the above steps will need to be adapted for other
   types of ARM host)
  (Note: for more info on your desired ARM host refer docs/arm/)
  (Note: you are free to change the ordering of above steps based
   on your workspace)
