#
# $Id: boot_images.txt,v 1.3 2006/03/04 22:47:41 jlivesey Exp $
#

NNGC Boot Images
----------------

  Boot Stages
  -----------

  NNGC has a multi-stage boot sequence, consisting of boot0, boot1 and
  boot2.

  + Boot0:  This is burned into ROM.  It is invoked at power-on and at
    reset.  It tries to read boot1 from flash, and does an ECC check
    of the flash image as it is written to sram memory.  If the ECC
    check passes, boot0 AES decrypts the boot1 image.  If the value of
    the SHA hash read from efuse is non-zero, boot0 does a SHA hash of
    the decrypted boot1 image and compares it with the saved hash from
    the efuse.

    If any of the tests above fail, boot0 will not attempt to execute
    boot1, but will start to flash a repeating sequnce of 00/1F on the
    LEDs.

    If all the tests above pass, boot0 will jump to boot1 and begin
    executing it.

  + Boot1:  This resides in Block 0 of the flash, which is guaranteed
    to be a good block, so that boot0 does not need to be aware of
    bad-block handling.

    Boot1 will try to read the image of Boot2, which is in the
    "logical" block #1 of flash.  This may be physical block #1 if it
    is a good block, or a later block if Block #1, #2.... are bad
    blocks.   Boot1 bad block handling is described below.

    Boot1 tries to read Boot2 from flash, and does an ECC check
    of the flash image as it is written to memory[*].

  + Boot2:  This resides in "logical" block #1 of flash, and can be
    any program you like.   During bringup we have used minimon0
    and also IOS as examples of Boot2.   

    [*] The POR is to write Boot2 from flash to Napa, but due to
    hardware limitations, in bringup Boot2 was actually written to
    DDR.


    Relocation of Images
    --------------------

    Each of the boot images above must be linked to relocate to the
    address of the memory where they are to be loaded and executed.

    + Boot1:  Relocated to Rom space, or 0xffff0000.  The lowest text
      address, 0xffff0000 must contain an ARM9 reset vector.   You
      can see the source of Boot0 in the ATI DV tree at:

	  main/test/sys/arm/boot0

    + Boot1:  Relocated to Sram space or 0xfff00000/0x0d400000.   The
      lowest text address must contain an ARM9 reset vector.   You
      can see the source of Boot1 in the IOS tree at:

	  sw/rvl/boot/boot1.

      Notice that in the Makefile you can define either of
      LOAD_TO_NAPA or LOAD_TO_DDR, which controls to which memory
      Boot2 will be loaded, and the boot1 source will automatically be
      built to match this.

    + Boot2:  Relocated to Napa or DDR space [**].  The lowest word of
      memory must contain a jump the to etxt segment entry point.

    [**] POR is Napa, but DDR is being used during bringup.


    Building Images
    ---------------

    Boot0:  The image of Boot0 was built before hardware bring-up and
    was burned into ROM.  Barring unforseen bugs, it is unlikely to
    change.

    Boot1:  The image of Boot1 can be changed, so long as you are
    prepared to go through a somwhat cumbersome process of build,
    followed by burning a flash image.  We have written some scripts
    that help with this process.

    + Build:  The Makefile in sw/rvl/boot/boot1 can be modified to 
      change the memory that Boot1 loads to, for example.

    + Flash image: Block #0 of the flash is guaranteed good, so the
      Boot1 flash image contains no bad block table.  Also, Boot1 is
      guaranteed to fit within one block, so no length header is
      required.

      A flash block contains 64 pages, each consisting of a 2k data
      part and a 64 byte spare area which contains four 32-bit ECC
      values - one per 512 byte section of the 2k data part - in its
      last sixteen bytes.  If you think of the spare area as 16 32-bit
      words rather than 64 bytes, the ECC values are in words
      spare[12:15].

      Given a .elf of the Boot1 code, the following invocation will
      create a flashable Block #0 image:

	     % makeimg boot1 0

    Boot2: The image of Boot2 is updated frequently.  Any progam can
    be Boot2 as long as it is relocated to run at the right location.
    If it is to be run in Napa, it should run at 0x11500 and if it is
    to run in DDR, it should run at 0x10000800.

    Because blocks after block #0 are not guaranteed good, and because
    Boot2 is not guaranteed to fit within one block, it needs two
    additional pieces of information.  Its first page will be a header
    page containing the size of the program in its first word - note:
    first data word, not first spare word.  Also, the first word of
    the spare area of pages #0 and #1 must contain the "good block"
    indicator, which is 0xff.  If pages #0 and #1 of the block do not
    contain this indicator, the entire block is regarded as bad by
    Boot1, which will skip it and look in block #2, and so on.

    Given an elf image, the following invocation will create a flash
    image to fit into Block #1 of flash.

	  % makeimg program_name 1

    [In this example we assume that Block #1 is in fact OK]


  Burning A Boot Image
  --------------------

  You never need to burn a Boot0 image, since it is in ROM and in
  general does not change, but you might want to replace Boot1 or
  Boot2 with your own version.  Fortunately, the minimon0 program has
  commands to help you do this.  There are a couple of ways to
  initiate minimon0, but in early bringup we used the Chameleon
  debugger, which recognizes pathnames on an attached PC:

      % load c:\username\minimon0.elf        // example pathname
	    [Then press the green GO button]

  The commands you need are those to reset the flash "cap", erase a
  block "erase", and write a boot image "wrb1" or "wrb2".  You can see
  minimon0's other commands by typing "help" at the command prompt.

  Before you can do any operation on the flash, you need to reset it
  using the "cap" command.  This command resets the flash and also
  outputs the parameters of the actual flash on the board.
  
  Before you write the pages of a block, you need to erase the block.
  If you don't, you will end up with bad bits and the ECC check will
  fail.  The erase command takes two arguments, the start and end
  block to erase.  If you want to erase a single block, they should be
  the same number.  So, to erase block #0, you type "erase 0 0".

  The commands to write Boot1 and Boot2 to flash are different.  They
  each take an associate filename, and if you are using Chameleon
  they can be pathnames to an attached PC.

      % wrb1 c:\username\pathname1 64

      % wrb2 c:\username\pathname2 64

  The 'pathname' for either of these should be the output from the
  makeimg command described above.

  After writing a boot1 or boot2 image to flash, you can verify that
  the write completed correctly using the "fv" command.

     % fv 0 0 63     // verifies block 0, pages 0 to 63

     % fv 1 0 63     // verifies block 1, pages 0 to 63


  Traps
  -----

  As a general comment, most of the tools that are used in creating
  boot images were originally developed as part of DV or bringup, so
  they are not polished or user-friendly.  As I find obvious traps I
  try to fix them, or at least issue a warning message.

  Trap 1: Bad image in memory
  ---------------------------

  Symptoms: the 'makeimg' script appears to complete correctly, and
  the flash image is written correctly, but when it is booted, it does
  not execute, and when you look at memory, for example using
  Chameleon, you find that your executable is in the wrong place, and
  begins with zeros, or perhaps with what looks like an ELF header.

  This is caused by the 'elftohex' program.  This is a general purpose
  conversion program that does not know it is dealing with a bootable
  image.

  The difference between a regular .elf file and a bootable .elf file
  is that a bootable .elf file must have its entry point and the
  beginning of its .text section at the same address.

  You can check if this is so by running 'elftohex' in non-conversion mode
  and dump out these two addresses:

     % elftohex -f yourfile.elf -t -e

  If the text section and the entry point are not the same address -
  which is typically 0x0d400000 for sram and 0x100000800 for DDR - you
  need to use a linker.dat script to get them to the same address.

  [Notice that none of the above applies if you are building a .elf
  file to be loaded into memory by the OS of by Chameleon.   It is
  only an issue for images that need to be ebooted out of flash by
  Boot0 or Boot1.]

  

# eof
