title: Executing bare metal qemu execution with -pflash command
body: <p>I have a website <a href="http://balau82.wordpress.com/2010/02/28/hello-world-for-bare-metal-arm-using-qemu/" rel="nofollow">Hello world for bare metal ARM using QEMU</a> that teaches how to run <code>qemu</code> for versatilePB. </p>
  
  <p>The website example uses <code>-kernel</code> option to load the binary image into 0x10000; I just assume that the binary is loaded into 0x10000 internally with the <code>-kernel</code>. </p>
  
  <p>This is the command <code>qemu-system-arm -M versatilepb -m 128M  -kernel test.bin -serial stdio</code>, and the source can be found at - <a href="https://dl.dropboxusercontent.com/u/10773282/2014/b1.zip" rel="nofollow">https://dl.dropboxusercontent.com/u/10773282/2014/b1.zip</a></p>
  
  <p>The ld setup is as follows:</p>
  
  <pre><code>ENTRY(_Reset)
  SECTIONS
  {
   . = 0x10000;
   .startup . : { startup.o(.text) } 
   ...
  }
  </code></pre>
  
  <p>Start up assembly is simple as follows:</p>
  
  <pre><code>.global _Reset
  _Reset:
   LDR sp, =stack_top
   BL c_entry
   B . 
  </code></pre>
  
  <p>The main c code (c_entry) is as follows:</p>
  
  <pre><code>volatile unsigned int * const UART0DR = (unsigned int *)0x101f1000;
  
  void print_uart0(const char *s) {
   while(*s != '\0') { /* Loop until end of string */
   *UART0DR = (unsigned int)(*s); /* Transmit char */
   s++; /* Next char */
   }
  }
  
  void c_entry() {
   print_uart0("Hello world!\n");
  }
  </code></pre>
  
  <p>I need to modify the code to boot without <code>-kernel</code>, but with <code>-pflash</code> to emulate as if the binary is read from the flash drive. This is my approach in trying to make it work:</p>
  
  <h3>Change the startup assembly and test.ld</h3>
  
  <p>I just used the other example from the same author of my example: <a href="http://balau82.wordpress.com/2010/02/14/simplest-bare-metal-program-for-arm/" rel="nofollow">http://balau82.wordpress.com/2010/02/14/simplest-bare-metal-program-for-arm/</a>
  This is the startup code:</p>
  
  <pre><code>.section INTERRUPT_VECTOR, "x"
  .global _Reset
  _Reset:
    B Reset_Handler /* Reset */
    B . /* Undefined */
    B . /* SWI */
    B . /* Prefetch Abort */
    B . /* Data Abort */
    B . /* reserved */
    B . /* IRQ */
    B . /* FIQ */
  
  Reset_Handler:
    LDR sp, =stack_top
    BL c_entry
    B .
  </code></pre>
  
  <p>This is the test.ld</p>
  
  <pre><code>ENTRY(_Reset)
  SECTIONS
  {
   . = 0x0;
   .text : {
   startup.o (INTERRUPT_VECTOR)
   *(.text)
   }
   .data : { *(.data) }
   .bss : { *(.bss COMMON) }
   . = ALIGN(8);
   . = . + 0x1000; /* 4kB of stack memory */
   stack_top = .;
  }
  </code></pre>
  
  <h3>Update the build code</h3>
  
  <p>After the build to get the test.bin, I used the <code>dd</code> command to create a flash binary. </p>
  
  <pre><code>arm-none-eabi-as -mcpu=arm926ej-s -g startup.s -o startup.o
  arm-none-eabi-gcc -c -mcpu=arm926ej-s -g test.c -o test.o
  arm-none-eabi-ld -T test.ld test.o startup.o -o test.elf
  arm-none-eabi-objcopy -O binary test.elf test.bin
  dd if=/dev/zero of=flash.bin bs=4096 count=4096
  dd if=test.bin of=flash.bin bs=4096 conv=notrunc
  </code></pre>
  
  <h3>qemu execution</h3>
  
  <p>Executed qemu to get this error message.</p>
  
  <pre><code>qemu-system-arm -M versatilepb -m 128M  -pflash flash.bin -nographic
  &gt;&gt; failed to read the initial flash content
  &gt;&gt; Initialization of device cfi.pflash01 failed
  </code></pre>
  
  <p>What might be wrong? I uploaded the examples and sample code.</p>
  
  <ul>
  <li>not working with -pflash: <a href="https://dl.dropboxusercontent.com/u/10773282/2014/b2.zip" rel="nofollow">https://dl.dropboxusercontent.com/u/10773282/2014/b2.zip</a></li>
  </ul>