This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

DM368 - K9F1G08U0B NAND Flashing

Expert 1840 points

Hi all,

I am working with a DM368 IPNC camera with using K9F1G08U0B PCB0 SamSung Flash memory.

I am using Nandwriter ultility of Appro to flash UBL-UBOOT and diagnostic but after burn successfully our camera cannot start (I could not see any message in terminal).  

I guess we did something wrong with nand partitions and  entryPoint + ldAddress setting.

I attached nandwriter.c, device_nand.h and 2 flashing logs as below. 

Starting DM36x_NANDWriter.
Attempting a global erase of NAND:(y/n)
y
Erasing block 0x0 through 0x3FE.
Enter the binary UBL file Name (enter 'none' to skip) :
d:\backup\development_tools\ipnc_arm\PrivateTI_IPNC_DM36x2.5\Utils\bin\dm368\ubl_432arm_340ddr_ipnc_dm368_1.1.0.bin
Number of blocks needed for header and data: 0x1
Attempting to start write in block number 0x1.
Unprotecting blocks 0x1 through 0x1.
Erasing block 0x1 through 0x4.
Writing header data to Block 0x1, Page 0x0
Writing image data to Block 0x4, Page 0x0
Writing image data to Block 0x4, Page 0x1
Writing image data to Block 0x4, Page 0x2
Writing image data to Block 0x4, Page 0x3
Writing image data to Block 0x4, Page 0x4
Writing image data to Block 0x4, Page 0x5
Writing image data to Block 0x4, Page 0x6
Writing image data to Block 0x4, Page 0x7
Writing image data to Block 0x4, Page 0x8
Writing image data to Block 0x4, Page 0x9
Writing image data to Block 0x4, Page 0xA
Protecting the entire NAND flash.
Enter the U-boot or application file name (enter 'none' to skip):
d:\backup\development_tools\ipnc_arm\PrivateTI_IPNC_DM36x2.5\Utils\bin\dm368\u-boot-1.3.4-dm368_ipnc_1.0.1.bin
Enter the U-boot or application entry point (in hex): 
0x81080000
Selected entry point is 0x81080000
Enter the U-boot or application load address (in hex): 
0x81080000
Number of blocks needed for header and data: 0x2
Attempting to start write in block number 0x8.
Unprotecting blocks 0x8 through 0x9.
Erasing block 0x8 through 0xC.
Writing header data to Block 0x8, Page 0x0
Writing image data to Block 0xB, Page 0x0
Writing image data to Block 0xB, Page 0x1
Writing image data to Block 0xB, Page 0x2
Writing image data to Block 0xB, Page 0x3
Writing image data to Block 0xB, Page 0x4
Writing image data to Block 0xB, Page 0x5
Writing image data to Block 0xB, Page 0x6
Writing image data to Block 0xB, Page 0x7
Writing image data to Block 0xB, Page 0x8
Writing image data to Block 0xB, Page 0x9
Writing image data to Block 0xB, Page 0xA
Writing image data to Block 0xB, Page 0xB
Writing image data to Block 0xB, Page 0xC
Writing image data to Block 0xB, Page 0xD
Writing image data to Block 0xB, Page 0xE
Writing image data to Block 0xB, Page 0xF
Writing image data to Block 0xB, Page 0x10
Writing image data to Block 0xB, Page 0x11
Writing image data to Block 0xB, Page 0x12
Writing image data to Block 0xB, Page 0x13
Writing image data to Block 0xB, Page 0x14
Writing image data to Block 0xB, Page 0x15
Writing image data to Block 0xB, Page 0x16
Writing image data to Block 0xB, Page 0x17
Writing image data to Block 0xB, Page 0x18
Writing image data to Block 0xB, Page 0x19
Writing image data to Block 0xB, Page 0x1A
Writing image data to Block 0xB, Page 0x1B
Writing image data to Block 0xB, Page 0x1C
Writing image data to Block 0xB, Page 0x1D
Writing image data to Block 0xB, Page 0x1E
Writing image data to Block 0xB, Page 0x1F
Writing image data to Block 0xB, Page 0x20
Writing image data to Block 0xB, Page 0x21
Writing image data to Block 0xB, Page 0x22
Writing image data to Block 0xB, Page 0x23
Writing image data to Block 0xB, Page 0x24
Writing image data to Block 0xB, Page 0x25
Writing image data to Block 0xB, Page 0x26
Writing image data to Block 0xB, Page 0x27
Writing image data to Block 0xB, Page 0x28
Writing image data to Block 0xB, Page 0x29
Writing image data to Block 0xB, Page 0x2A
Writing image data to Block 0xB, Page 0x2B
Writing image data to Block 0xB, Page 0x2C
Writing image data to Block 0xB, Page 0x2D
Writing image data to Block 0xB, Page 0x2E
Writing image data to Block 0xB, Page 0x2F
Writing image data to Block 0xB, Page 0x30
Writing image data to Block 0xB, Page 0x31
Writing image data to Block 0xB, Page 0x32
Writing image data to Block 0xB, Page 0x33
Writing image data to Block 0xB, Page 0x34
Writing image data to Block 0xB, Page 0x35
Writing image data to Block 0xB, Page 0x36
Writing image data to Block 0xB, Page 0x37
Writing image data to Block 0xB, Page 0x38
Writing image data to Block 0xB, Page 0x39
Writing image data to Block 0xB, Page 0x3A
Writing image data to Block 0xB, Page 0x3B
Writing image data to Block 0xB, Page 0x3C
Writing image data to Block 0xB, Page 0x3D
Writing image data to Block 0xB, Page 0x3E
Writing image data to Block 0xB, Page 0x3F
Writing image data to Block 0xC, Page 0x0
Writing image data to Block 0xC, Page 0x1
Writing image data to Block 0xC, Page 0x2
Writing image data to Block 0xC, Page 0x3
Writing image data to Block 0xC, Page 0x4
Writing image data to Block 0xC, Page 0x5
Writing image data to Block 0xC, Page 0x6
Writing image data to Block 0xC, Page 0x7
Writing image data to Block 0xC, Page 0x8
Writing image data to Block 0xC, Page 0x9
Writing image data to Block 0xC, Page 0xA
Writing image data to Block 0xC, Page 0xB
Protecting the entire NAND flash.
Erasing block 0x1B through 0x1F.
Erasing block 0x300 through 0x3FF.
Enter the Diagnostic file name (enter 'none' to skip):
d:\backup\development_tools\ipnc_arm\PrivateTI_IPNC_DM36x2.5\Utils\bin\dm368\diagnostic_ipnc_dm368_1.0.0.bin
Number of blocks needed for header and data: 0x2
Attempting to start write in block number 0x18.
Unprotecting blocks 0x18 through 0x19.
Erasing block 0x18 through 0x1C.
Writing header data to Block 0x18, Page 0x0
Writing image data to Block 0x1B, Page 0x0
Writing image data to Block 0x1B, Page 0x1
Writing image data to Block 0x1B, Page 0x2
Writing image data to Block 0x1B, Page 0x3
Writing image data to Block 0x1B, Page 0x4
Writing image data to Block 0x1B, Page 0x5
Writing image data to Block 0x1B, Page 0x6
Writing image data to Block 0x1B, Page 0x7
Writing image data to Block 0x1B, Page 0x8
Writing image data to Block 0x1B, Page 0x9
Writing image data to Block 0x1B, Page 0xA
Writing image data to Block 0x1B, Page 0xB
Writing image data to Block 0x1B, Page 0xC
Writing image data to Block 0x1B, Page 0xD
Writing image data to Block 0x1B, Page 0xE
Writing image data to Block 0x1B, Page 0xF
Writing image data to Block 0x1B, Page 0x10
Writing image data to Block 0x1B, Page 0x11
Writing image data to Block 0x1B, Page 0x12
Writing image data to Block 0x1B, Page 0x13
Writing image data to Block 0x1B, Page 0x14
Writing image data to Block 0x1B, Page 0x15
Writing image data to Block 0x1B, Page 0x16
Writing image data to Block 0x1B, Page 0x17
Writing image data to Block 0x1B, Page 0x18
Writing image data to Block 0x1B, Page 0x19
Writing image data to Block 0x1B, Page 0x1A
Writing image data to Block 0x1B, Page 0x1B
Writing image data to Block 0x1B, Page 0x1C
Writing image data to Block 0x1B, Page 0x1D
Writing image data to Block 0x1B, Page 0x1E
Writing image data to Block 0x1B, Page 0x1F
Writing image data to Block 0x1B, Page 0x20
Writing image data to Block 0x1B, Page 0x21
Writing image data to Block 0x1B, Page 0x22
Writing image data to Block 0x1B, Page 0x23
Writing image data to Block 0x1B, Page 0x24
Writing image data to Block 0x1B, Page 0x25
Writing image data to Block 0x1B, Page 0x26
Writing image data to Block 0x1B, Page 0x27
Writing image data to Block 0x1B, Page 0x28
Writing image data to Block 0x1B, Page 0x29
Writing image data to Block 0x1B, Page 0x2A
Writing image data to Block 0x1B, Page 0x2B
Writing image data to Block 0x1B, Page 0x2C
Writing image data to Block 0x1B, Page 0x2D
Writing image data to Block 0x1B, Page 0x2E
Writing image data to Block 0x1B, Page 0x2F
Writing image data to Block 0x1B, Page 0x30
Writing image data to Block 0x1B, Page 0x31
Writing image data to Block 0x1B, Page 0x32
Writing image data to Block 0x1B, Page 0x33
Writing image data to Block 0x1B, Page 0x34
Writing image data to Block 0x1B, Page 0x35
Writing image data to Block 0x1B, Page 0x36
Writing image data to Block 0x1B, Page 0x37
Writing image data to Block 0x1B, Page 0x38
Writing image data to Block 0x1B, Page 0x39
Writing image data to Block 0x1B, Page 0x3A
Writing image data to Block 0x1B, Page 0x3B
Writing image data to Block 0x1B, Page 0x3C
Writing image data to Block 0x1B, Page 0x3D
Writing image data to Block 0x1B, Page 0x3E
Writing image data to Block 0x1B, Page 0x3F
Writing image data to Block 0x1C, Page 0x0
Writing image data to Block 0x1C, Page 0x1
Writing image data to Block 0x1C, Page 0x2
Writing image data to Block 0x1C, Page 0x3
Writing image data to Block 0x1C, Page 0x4
Writing image data to Block 0x1C, Page 0x5
Writing image data to Block 0x1C, Page 0x6
Writing image data to Block 0x1C, Page 0x7
Writing image data to Block 0x1C, Page 0x8
Writing image data to Block 0x1C, Page 0x9
Writing image data to Block 0x1C, Page 0xA
Protecting the entire NAND flash.


NAND boot preparation was successful!

/* --------------------------------------------------------------------------
  FILE        : device_nand.h
  PROJECT     : TI Booting and Flashing Utilities
  AUTHOR      : Daniel Allred
  DESC        : Provides device differentiation for the project files. This
                file MUST be modified to match the device specifics.
----------------------------------------------------------------------------- */

#ifndef _DEVICE_NAND_H_
#define _DEVICE_NAND_H_

#include "tistdtypes.h"

// Prevent C++ name mangling
#ifdef __cplusplus
extern far "c" {
#endif

/***********************************************************
* Global Macro Declarations                                *
***********************************************************/

#define DEVICE_NAND_DATA_OFFSET        (0x00000000u)
#define DEVICE_NAND_ALE_OFFSET         (0x00000008u)
#define DEVICE_NAND_CLE_OFFSET         (0x00000010u)
#define DEVICE_NAND_TIMEOUT            (10240)

#define DEVICE_NAND_MAX_BYTES_PER_OP       (512)   // Max Bytes per operation (EMIF IP constrained)
#define DEVICE_NAND_MAX_SPAREBYTES_PER_OP  (16)    // Max Spare Bytes per operation
#define DEVICE_NAND_MIN_SPAREBYTES_PER_OP  (10)    // Min Spare Bytes per operation (ECC operation constrained)

#ifdef IPNC_DM365
// Defines which NAND blocks the RBL will search in for a UBL image
#define DEVICE_NAND_RBL_SEARCH_START_BLOCK     (1)
#define DEVICE_NAND_RBL_SEARCH_END_BLOCK       (3)

// Defines which NAND blocks are valid for writing the APP data
#define DEVICE_NAND_UBL_SEARCH_START_BLOCK     (8)
#define DEVICE_NAND_UBL_SEARCH_END_BLOCK       (10)

// Defines which NAND blocks are valid for writing the Diagnostic data
#define DEVICE_NAND_DBL_SEARCH_START_BLOCK     (32)
#define DEVICE_NAND_DBL_SEARCH_END_BLOCK       (34)

#elif defined(IPNC_DM368)
// Defines which NAND blocks the RBL will search in for a UBL image
#define DEVICE_NAND_RBL_SEARCH_START_BLOCK     (1)
#define DEVICE_NAND_RBL_SEARCH_END_BLOCK       (3)

// Defines which NAND blocks are valid for writing the APP data
#define DEVICE_NAND_UBL_SEARCH_START_BLOCK     (8)
#define DEVICE_NAND_UBL_SEARCH_END_BLOCK       (10)

// Defines which NAND blocks are valid for writing the Diagnostic data
#define DEVICE_NAND_DBL_SEARCH_START_BLOCK     (24)
#define DEVICE_NAND_DBL_SEARCH_END_BLOCK       (26)

#else
// Defines which NAND blocks the RBL will search in for a UBL image
#define DEVICE_NAND_RBL_SEARCH_START_BLOCK     (1)
#define DEVICE_NAND_RBL_SEARCH_END_BLOCK       (24)

// Defines which NAND blocks are valid for writing the APP data
#define DEVICE_NAND_UBL_SEARCH_START_BLOCK     (25)
#define DEVICE_NAND_UBL_SEARCH_END_BLOCK       (50)

#endif

/******************************************************
* Global Typedef declarations                         *
******************************************************/


/***********************************************************
* Global Function Declarations                             *
***********************************************************/


/***********************************************************
* End file                                                 *
***********************************************************/

#ifdef __cplusplus
}
#endif

#endif // End _DEVICE_NAND_H_

/* --------------------------------------------------------------------------
  FILE        : nandwriter.c 				                             	 	        
  PROJECT     : TI Booting and Flashing Utilities
  AUTHOR      : Sandeep Paulraj
  DESC	      : CCS-based utility to flash the DM36x in preparation for 
                NAND booting
 ----------------------------------------------------------------------------- */

// C standard I/O library
#include "stdio.h"

// General type include
#include "tistdtypes.h"

// Device specific CSL
#include "device.h"

// This module's header file 
#include "nandwriter.h"

// NAND driver include
#include "nand.h"
#include "device_nand.h"

// Misc. utility function include
#include "util.h"


/************************************************************
* Explicit External Declarations                            *
************************************************************/

extern __FAR__ Uint32 EMIFStart;
extern __FAR__ Uint32 DDRStart;


/************************************************************
* Local Macro Declarations                                  *
************************************************************/


/************************************************************
* Local Typedef Declarations                                *
************************************************************/


/************************************************************
* Local Function Declarations                               *
************************************************************/

static Uint32 nandwriter(void);
static Uint32 LOCAL_writeHeaderAndData(NAND_InfoHandle hNandInfo, NANDWRITER_Boot *nandBoot, Uint8 *srcBuf);


/************************************************************
* Global Variable Definitions
************************************************************/

// Global variables for page buffers 
static Uint8* gNandTx;
static Uint8* gNandRx;

/************************************************************
* Global Function Definitions                               *
************************************************************/

void main( void )
{
  int status;

  // Init memory alloc pointer
  UTIL_setCurrMemPtr(0);

#if defined(IPNC_DM365) || defined(IPNC_DM368)
  DEVICE_pinmuxControl(2,0xFFFFFFFF,0x00000000);  // EMIFA
#endif
  // System init
  if (DEVICE_init() !=E_PASS)
  {
    exit();
  }
//  DEVICE_pinmuxControl( 0, DEVICE_PINMUX_EMIF_MASK, DEVICE_PINMUX_EMIF_EN );

	// Execute the NAND flashing
  status = nandwriter();

  if (status != E_PASS)
  {
    DEBUG_printString("\n\nNAND flashing failed!\r\n");
  }
  else
  {
    DEBUG_printString( "\n\nNAND boot preparation was successful!\r\n" );
  }
}


/************************************************************
* Local Function Definitions                                *
************************************************************/

static Uint32 nandwriter()
{
  Uint32 numPagesUBL;
  Uint32 numPagesAPP;

  NANDWRITER_Boot  gNandBoot;
  NAND_InfoHandle  hNandInfo;

  FILE	*fPtr;
  Uint8	*ublPtr, *appPtr;
  Int32	ublFileSize = 0,ublAllocSize = 0, appFileSize = 0,appAllocSize = 0;
  Int8  fileName[256];
  Int8  answer[24];
  Int32 i=0;

  DEBUG_printString("Starting DM36x_NANDWriter.\r\n");

  // Initialize NAND Flash
  hNandInfo = NAND_open((Uint32)&EMIFStart, (Uint8) DEVICE_emifBusWidth() );
  
  if (hNandInfo == NULL)
  {
    DEBUG_printString( "\tERROR: NAND Initialization failed.\r\n" );
    return E_FAIL;
  }
#if defined(IPNC_DM365) || defined(IPNC_DM368)
  DEBUG_printString("Attempting a global erase of NAND:(y/n)\r\n");
  DEBUG_readString(answer);
  if(strcmp(answer,"y")==0){
  	NAND_globalErase(hNandInfo);
  }
#endif
  // Read the file from host
  DEBUG_printString("Enter the binary UBL file Name (enter 'none' to skip) :\r\n");
  DEBUG_readString(fileName);
  fflush(stdin);

  if (strcmp(fileName,"none") != 0)
  {
	// Open an File from the hard drive
	fPtr = fopen(fileName, "rb");
	if(fPtr == NULL)
	{
      DEBUG_printString("\tERROR: File ");
      DEBUG_printString(fileName);
      DEBUG_printString(" open failed.\r\n");
      return E_FAIL;
	}

    // Read file size
    fseek(fPtr,0,SEEK_END);
    ublFileSize = ftell(fPtr);

    if(ublFileSize == 0)
    {
      DEBUG_printString("\tERROR: File read failed.. Closing program.\r\n");
      fclose (fPtr);
      return E_FAIL;
    }

    numPagesUBL = 0;
    while ( (numPagesUBL * hNandInfo->dataBytesPerPage)  < ublFileSize )
    {
      numPagesUBL++;
    }

    //We want to allocate an even number of pages.
    ublAllocSize = numPagesUBL * hNandInfo->dataBytesPerPage;

    // Setup pointer in RAM
    ublPtr = (Uint8 *) UTIL_allocMem(ublAllocSize);


    for (i=0; i<ublAllocSize; i++)
      ublPtr[i]=0x00;

    fseek(fPtr,0,SEEK_SET);

    if (ublFileSize != fread(ublPtr, 1, ublFileSize, fPtr))
    {
      DEBUG_printString("\tWARNING: File Size mismatch.\r\n");
    }

    fclose (fPtr);

    gNandBoot.magicNum    = UBL_MAGIC_SAFE;
    gNandBoot.block       = DEVICE_NAND_RBL_SEARCH_START_BLOCK;
    gNandBoot.page        = 0;
    gNandBoot.numPage     = numPagesUBL;
    gNandBoot.entryPoint  = 0x0100;       // This fixed entry point will work with the UBLs
    gNandBoot.ldAddress   = 0;            // This doesn't matter for the UBL

    if (LOCAL_writeHeaderAndData(hNandInfo,&gNandBoot,ublPtr) != E_PASS)
    {
      printf("\tERROR: Write failed.\r\n");
      return E_FAIL;
    }
  }

  // Read the file from host
  DEBUG_printString("Enter the U-boot or application file name (enter 'none' to skip):\r\n");
  DEBUG_readString(fileName);
  fflush(stdin);

  if (strcmp(fileName,"none") != 0)
  {
    // Open an File from the hard drive
    fPtr = fopen(fileName, "rb");
    if(fPtr == NULL)
    {
      DEBUG_printString("\tERROR: File ");
      DEBUG_printString(fileName);
      DEBUG_printString(" open failed.\r\n");
      return E_FAIL;
    }

    // Read file size
    fseek(fPtr,0,SEEK_END);
    appFileSize = ftell(fPtr);

    if(appFileSize == 0)
    {
      DEBUG_printString("\tERROR: File read failed.. Closing program.\r\n");
      fclose (fPtr);
      return E_FAIL;
    }

    numPagesAPP = 0;
    while ( (numPagesAPP * hNandInfo->dataBytesPerPage)  < (appFileSize) )
    {
      numPagesAPP++;
    }

    // We want to allocate an even number of pages.
    appAllocSize = numPagesAPP * hNandInfo->dataBytesPerPage;
     
    // Setup pointer in RAM
    appPtr = (Uint8 *) UTIL_allocMem(appAllocSize);

    fseek(fPtr,0,SEEK_SET);

    if (appFileSize != fread(appPtr, 1, appFileSize, fPtr))
    {
      DEBUG_printString("\tWARNING: File Size mismatch\n");
    }

    fclose(fPtr);

    // Get the entry point and load addresses
    DEBUG_printString("Enter the U-boot or application entry point (in hex): \n");
    DEBUG_readString(answer);
    gNandBoot.entryPoint = strtoul(answer, NULL, 16);
    fflush(stdin);

    if ( (gNandBoot.entryPoint < DEVICE_DDR2_START_ADDR) || (gNandBoot.entryPoint >= DEVICE_DDR2_END_ADDR) )
    {
      DEBUG_printString("\tWARNING: Entry point not in acceptable range - using default 0x81080000.\r\n");
      gNandBoot.entryPoint = 0x81080000;
    }
    else
    {
      DEBUG_printString("Selected entry point is ");
      DEBUG_printHexInt(gNandBoot.entryPoint);
      DEBUG_printString("\r\n");
    }

    DEBUG_printString("Enter the U-boot or application load address (in hex): \r\n");
    DEBUG_readString(answer);
    gNandBoot.ldAddress = strtoul(answer, NULL, 16);

    if ( (gNandBoot.ldAddress < DEVICE_DDR2_START_ADDR) || (gNandBoot.ldAddress >= DEVICE_DDR2_END_ADDR) )
    {
      DEBUG_printString("\tWARNING: Load address not in acceptable range - using default 0x81080000.\r\n");
      gNandBoot.ldAddress = 0x81080000;
    }

//   gNandBoot.magicNum = UBL_MAGIC_SAFE;
	gNandBoot.magicNum = UBL_MAGIC_BIN_IMG;
    gNandBoot.block = DEVICE_NAND_UBL_SEARCH_START_BLOCK;
    gNandBoot.page = 0;
    gNandBoot.numPage = numPagesAPP;

    if (LOCAL_writeHeaderAndData(hNandInfo, &gNandBoot, appPtr) != E_PASS)
    {
      DEBUG_printString("\tERROR: Write Failed\n");
      return E_FAIL;
    }

#if defined(IPNC_DM365) || defined(IPNC_DM368)
	if (NAND_eraseBlocks_with_bb_check(hNandInfo,27,5) != E_PASS)
	{
	    DEBUG_printString("Erase failed\n");
		return E_FAIL;
	}
	if (NAND_eraseBlocks_with_bb_check(hNandInfo,hNandInfo->numBlocks-256,256) != E_PASS)
	{
	    DEBUG_printString("Erase failed\n");
		return E_FAIL;
	}	
 }
  // Read the file from host
  DEBUG_printString("Enter the Diagnostic file name (enter 'none' to skip):\r\n");
  DEBUG_readString(fileName);
  fflush(stdin);

  if (strcmp(fileName,"none") != 0)
  {
    // Open an File from the hard drive
    fPtr = fopen(fileName, "rb");
    if(fPtr == NULL)
    {
      DEBUG_printString("\tERROR: File ");
      DEBUG_printString(fileName);
      DEBUG_printString(" open failed.\r\n");
      return E_FAIL;
    }

    // Read file size
    fseek(fPtr,0,SEEK_END);
    appFileSize = ftell(fPtr);

    if(appFileSize == 0)
    {
      DEBUG_printString("\tERROR: File read failed.. Closing program.\r\n");
      fclose (fPtr);
      return E_FAIL;
    }

    numPagesAPP = 0;
    while ( (numPagesAPP * hNandInfo->dataBytesPerPage)  < (appFileSize) )
    {
      numPagesAPP++;
    }

    // We want to allocate an even number of pages.
    appAllocSize = numPagesAPP * hNandInfo->dataBytesPerPage;
     
    // Setup pointer in RAM
    appPtr = (Uint8 *) UTIL_allocMem(appAllocSize);

    fseek(fPtr,0,SEEK_SET);

    if (appFileSize != fread(appPtr, 1, appFileSize, fPtr))
    {
      DEBUG_printString("\tWARNING: File Size mismatch\n");
    }

    fclose(fPtr);

    // set the entry point and load addresses
	gNandBoot.entryPoint =0x81080040;
    //set load address
    gNandBoot.ldAddress = 0x81080000;

	gNandBoot.magicNum = UBL_MAGIC_BIN_IMG;
    gNandBoot.block = DEVICE_NAND_DBL_SEARCH_START_BLOCK;
    gNandBoot.page = 0;
    gNandBoot.numPage = numPagesAPP;

    if (LOCAL_writeHeaderAndData(hNandInfo, &gNandBoot, appPtr) != E_PASS)
    {
      DEBUG_printString("\tERROR: Write Failed\n");
      return E_FAIL;
    }
#endif
  }
  return E_PASS;
}

// Generic function to write a UBL or Application header and the associated data
static Uint32 LOCAL_writeHeaderAndData(NAND_InfoHandle hNandInfo, NANDWRITER_Boot *nandBoot, Uint8 *srcBuf)
{
  Uint32    endBlockNum;
  Uint32    *headerPtr;
  Uint32    blockNum;
  Uint32    count;
  Uint32    countMask;
  Uint32    numBlks;
  Uint32    pageNum;
  Uint32    i;
  Uint8     *dataPtr;


  gNandTx = (Uint8 *) UTIL_allocMem(NAND_MAX_PAGE_SIZE);
  gNandRx = (Uint8 *) UTIL_allocMem(NAND_MAX_PAGE_SIZE);

  for (i=0; i<NAND_MAX_PAGE_SIZE; i++)  
  {
    gNandTx[i]=0xff;
    gNandRx[i]=0xff;
  }  
  
  // Get total number of blocks needed
  numBlks = 0;
  while ( (numBlks * hNandInfo->pagesPerBlock)  < (nandBoot->numPage + 1) )
  {
    numBlks++;
  }
  DEBUG_printString("Number of blocks needed for header and data: ");
  DEBUG_printHexInt(numBlks);
  DEBUG_printString("\r\n");

  // Check whether writing UBL or APP (based on destination block)
  blockNum = nandBoot->block;
  if (blockNum == DEVICE_NAND_RBL_SEARCH_START_BLOCK)
  {
    endBlockNum = DEVICE_NAND_RBL_SEARCH_END_BLOCK;
  }
  else if (blockNum == DEVICE_NAND_UBL_SEARCH_START_BLOCK)
  {
    endBlockNum = DEVICE_NAND_UBL_SEARCH_END_BLOCK;
  }
#if defined(IPNC_DM365) || defined(IPNC_DM368)
  else if (blockNum == DEVICE_NAND_DBL_SEARCH_START_BLOCK)
  {
    endBlockNum = DEVICE_NAND_DBL_SEARCH_END_BLOCK;
  }
#endif
  else
  {
    // Block number is out of range
    return E_FAIL; 
  }

NAND_WRITE_RETRY:
  if (blockNum > endBlockNum)
  {
    return E_FAIL;
  }
  DEBUG_printString("Attempting to start write in block number ");
  DEBUG_printHexInt(blockNum);
  DEBUG_printString(".\r\n");

  // Unprotect all needed blocks of the Flash 
  if (NAND_unProtectBlocks(hNandInfo,blockNum,numBlks) != E_PASS)
  {
    blockNum++;
    DEBUG_printString("Unprotect failed.\r\n");
    goto NAND_WRITE_RETRY;
  }
  
  // Setup header to be written
  headerPtr = (Uint32 *) gNandTx;
  headerPtr[0] = nandBoot->magicNum;          //Magic Number
  headerPtr[1] = nandBoot->entryPoint;        //Entry Point
  headerPtr[2] = nandBoot->numPage;           //Number of Pages
#if defined(IPNC_DM365) || defined(IPNC_DM368)
  headerPtr[3] = blockNum+3;                  //Starting Block Number
   headerPtr[4] = 0;                           //Starting Page Number - always start data in page 1 (this header goes in page 0)
#else
  headerPtr[3] = blockNum;                    //Starting Block Number 
  headerPtr[4] = 1;                           //Starting Page Number - always start data in page 1 (this header goes in page 0)

#endif   

  if ( (blockNum>=DEVICE_NAND_RBL_SEARCH_START_BLOCK) &&  (blockNum <= DEVICE_NAND_RBL_SEARCH_END_BLOCK) )
  {
    headerPtr[5] = 0;                           //nandBoot->ldAddress;  
  }
  else if ( (blockNum>=DEVICE_NAND_UBL_SEARCH_START_BLOCK) &&  (blockNum<=DEVICE_NAND_UBL_SEARCH_END_BLOCK) )
  {
    headerPtr[5] = nandBoot->ldAddress;         //nandBoot->ldAddress;
  }
#if defined(IPNC_DM365) || defined(IPNC_DM368)
  else if ( (blockNum>=DEVICE_NAND_DBL_SEARCH_START_BLOCK) &&  (blockNum<=DEVICE_NAND_DBL_SEARCH_END_BLOCK) )
  {
    headerPtr[5] = nandBoot->ldAddress;         //nandBoot->ldAddress;
  }
#endif
  else
  {
    // Block number is out of range
    return E_FAIL; 
  }
  pageNum = 0;
    
  // Erase the block where the header goes and the data starts
#if defined(IPNC_DM365) || defined(IPNC_DM368)
  if (NAND_eraseBlocks(hNandInfo,blockNum,numBlks+3) != E_PASS)
#else
  if (NAND_eraseBlocks(hNandInfo,blockNum,numBlks) != E_PASS)
#endif
  {
    blockNum++;
    DEBUG_printString("Erase failed\n");
    goto NAND_WRITE_RETRY;
  }

  DEBUG_printString("Writing header data to Block ");
  DEBUG_printHexInt(blockNum);
  DEBUG_printString(", Page ");
  DEBUG_printHexInt(pageNum);
  DEBUG_printString("\r\n");

  if (NAND_writePage(hNandInfo, blockNum, pageNum, gNandTx) != E_PASS)
  {
    blockNum++;
    DEBUG_printString("Write failed\n");
    NAND_reset(hNandInfo);
    goto NAND_WRITE_RETRY;
  }
    
  UTIL_waitLoop(200);

  // Verify the page just written
  if (NAND_verifyPage(hNandInfo, blockNum, pageNum, gNandTx, gNandRx) != E_PASS)
  {
    DEBUG_printString("Verify failed. Attempting to clear page\n");
    NAND_reset(hNandInfo);
    NAND_eraseBlocks(hNandInfo,blockNum,numBlks);
    
    blockNum++;
    NAND_reset(hNandInfo);

    goto NAND_WRITE_RETRY;
  }

  // Start writing in page 1 of current block (header was in page 0)
  count = 1;
#if defined(IPNC_DM365) || defined(IPNC_DM368)
  count = 0;
  blockNum+=3;
#endif
  // The following assumes power of 2 pagesPerBlock -  *should* always be valid 
  countMask = (Uint32) hNandInfo->pagesPerBlock - 1;
  dataPtr = srcBuf;

  do
  {
    DEBUG_printString((Uint8 *)"Writing image data to Block ");
    DEBUG_printHexInt(blockNum);
    DEBUG_printString((Uint8 *)", Page ");
    DEBUG_printHexInt(count & countMask);
    DEBUG_printString((Uint8 *)"\r\n");

    // Write the UBL or APP data on a per page basis
    if (NAND_writePage(hNandInfo, blockNum,  (count & countMask), dataPtr) != E_PASS)
    {
      blockNum++;
      DEBUG_printString("Write failed\n");
      goto NAND_WRITE_RETRY;
    }
    
    UTIL_waitLoop(200);
    
    // Verify the page just written
    if (NAND_verifyPage(hNandInfo, blockNum, (count & countMask), dataPtr, gNandRx) != E_PASS)
    {
      DEBUG_printString("Verify failed. Attempting to clear page\n");
      NAND_reset(hNandInfo);
      NAND_eraseBlocks(hNandInfo,blockNum,numBlks);
      blockNum++;
      goto NAND_WRITE_RETRY;
    }
    
    count++;
    dataPtr +=  hNandInfo->dataBytesPerPage;
    if (!(count & countMask))
    {
      do
      {
        blockNum++;
      }
      while (NAND_badBlockCheck(hNandInfo,blockNum) != E_PASS);
    }
  } while (count <= nandBoot->numPage);

  NAND_protectBlocks(hNandInfo);

  return E_PASS;
}


/***********************************************************
* End file                                                 *
***********************************************************/

/* --------------------------------------------------------------------------
  HISTORY
    v1.00 - DJA - 02-Nov-2007
      Initial release
-------------------------------------------------------------------------- */

please give me a hand to figure out the issue.

Thank you very much!