title: Booting kernel from SD in qemu (ARM) with u-boot
body: <p>I'm quite new to embedded systems and I'm playing around with ARM on qemu. So I've run into problem booting linux kernel image from an emulated SD on versatile express with cpu cortex-a9.</p>
  
  <p>I prepared everything in the following order: first, I've built the kernel with <code>vexpress_defconfig</code> using appropriate ARM toolchain. Then I've built u-boot with <code>vexpress_ca9x4_defconfig</code>. Everything went just fine. The linux kernel source I took is at version 4.13 (latest stable from git). U-boot version is 2017.09-00100. Then I prepared an SD image:</p>
  
  <pre><code>dd if=/dev/zero of=sd.img bs=4096 count=4096
  mkfs.vfat sd.img
  mount sd.img /mnt/tmp -o loop,rw
  cp kernel/arch/arm/boot/zImage /mnt/tmp
  umount /mnt/tmp
  </code></pre>
  
  <p>Next I try to run qemu as follows:</p>
  
  <pre><code>qemu-system-arm -machine vexpress-a9 -cpu cortex-a9 -m 128M -dtb kernel/arch/arm/boot/dts/vexpress-v2p-ca9.dtb -kernel uboot/u-boot -sd sd.img -nographic
  </code></pre>
  
  <p>U-boot loads successfully and gives me command prompt. And SD is really there and attached:</p>
  
  <pre><code>=&gt; mmcinfo
  Device: MMC
  Manufacturer ID: aa
  OEM: 5859
  Name: QEMU! 
  Tran Speed: 25000000
  Rd Block Len: 512
  SD version 1.0
  High Capacity: No
  Capacity: 16 MiB
  Bus Width: 1-bit
  Erase Group Size: 512 Bytes
  </code></pre>
  
  <p>I attempt to load compressed image from SD into memory and boot it:</p>
  
  <pre><code>fatload mmc 0:0 0x4000000 zImage
  </code></pre>
  
  <p>and everything looks ok</p>
  
  <pre><code>=&gt; fatload mmc 0:0 0x4000000 zImage
  reading zImage
  3378968 bytes read in 1004 ms (3.2 MiB/s)
  </code></pre>
  
  <p>but then I want to boot the kernel and get an error:</p>
  
  <pre><code>=&gt; bootz 0x4000000
  Bad Linux ARM zImage magic!
  </code></pre>
  
  <p>I also tried U-boot images, crafted with u-boot's <code>mkimage</code>, like in this example:</p>
  
  <pre><code>uboot/tools/mkimage -A arm -C none -O linux -T kernel -d kernel/arch/arm/boot/Image -a 0x00010000 -e 0x00010000 uImage
  </code></pre>
  
  <p>also trying out <code>-C gzip</code> on <code>zImage</code> and different load/entry addresses, to no avail. The images were copied to <code>sd.img</code>. When I <code>fatload</code> the image and check it with <code>iminfo</code>, whichever options I try, I constantly get error:</p>
  
  <pre><code>=&gt; iminfo 0x4000000
  
  ## Checking Image at 04000000 ...
  Unknown image format!
  </code></pre>
  
  <p>I'm totally confused and this problem drives me nuts, while information on this subject in Internet is rather scarce. Please, hint me what I'm doing wrong and redirect into right direction.</p>
  
  <p>qemu in use is <code>QEMU emulator version 2.9.0</code>.</p>