dl_proc.c

来自「在高通的手机平台下,一个下载手机.bin文件到手机的flash中的工具,包含PC」· C语言 代码 · 共 1,215 行 · 第 1/3 页

C
1,215
字号
/*===========================================================================

					 INCLUDE FILES FOR MODULE

===========================================================================*/
/*
 when        who                 what, where, why
 --------  --------     ---------------------------------------------------------------
02/09/06  wufei.lin        modify to support dynamic flash support
12/26/05  wufei.lin        add samsung flash
 01/27/05  nony.wu      Added change baud rate command
 ============================================================================ */

#include "dl_comm.h"
#include "dl_uart.h"
#include "dl_proc.h"
#include "dl_flashdrv.h"



/*===========================================================================

			LOCAL DEFINITIONS AND DECLARATIONS FOR MODULE

This section contains local definitions for constants, macros, types,
variables and other items needed by this module.

===========================================================================*/

/* The Download Protocol is enhanced from time to time.  This value defines
   the version number of the protocol supported by this implementation. */
#define  PROTOCOL_ID          4

/* Someday the change might not be backwards compatible.  When that happens,
   this value will define the earliest protocol spec with which this
   implementation is compatible.  So far, all versions are compatible, so
   this value is 1, which indicates that it is compatible with all versions. */
#define  MIN_PROTOCOL_ID      1

/* Async HDLC achieves data transparency at the byte level by using
   two special values. The first is a flag value which begins and ends
   every packet: */
#define  ASYNC_HDLC_FLAG      0x7e

/* The flag value might appear in the data.  If it does, it is sent as
   a two-byte sequence consisting of a special escape value followed by
   the flag value XORed with 0x20.  This gives a special meaning to the
   escape character, so if it appears in the data it is itself escaped
   in the same way. */
#define  ASYNC_HDLC_ESC       0x7d
#define  ASYNC_HDLC_ESC_MASK  0x20

/*-------------------------------------------------------------------------*/

/* Maximum size of a received packet. */

#define  MAX_PACKET_LEN    (0x2000) 

/* Minimum size of a received packet. */
#define  MIN_PACKET_LEN    3        /* 2 bytes CRC + 1 byte command code */

/* According to the protocol spec, a version string can be at most 20 bytes */
#define  MAX_VERSTRING_LEN    (20)

#define  ROOM_FOR_CRC   (4)         /* Allow this much room for the CRC,
                                       including worst-case expansion due
                                       to byte-stuffing */
#define  ROOM_FOR_FLAG  (1)         /* Allow this much room for trailing flag */

/* The buffer size for response packets.  Based on the longest possible
   packet, the Software Version acknowledgement. */
#define  MAX_RESPONSE_LEN  (1+2+MAX_VERSTRING_LEN+ROOM_FOR_CRC+ROOM_FOR_FLAG)
/* 1 = command code
   2 = fixed fields of packet, version and len */
#define  SVC_Stack_Size  0x200
int   svc_stack[SVC_Stack_Size];
/*-------------------------------------------------------------------------*/
/* Each packet sent or received in this download protocol begins with a
   one-byte command code chosen from the following list.  Each packet
   type is either received by the phone or transmitted by the phone, but
   not both. */

typedef PACKED enum
{
	CMD_WRITE  = 0x01,	 /* Write a block of data to memory (received)    */
	CMD_ACK    = 0x02,	 /* Acknowledge receiving a packet  (transmitted) */
	CMD_NAK    = 0x03,	 /* Acknowledge a bad packet        (transmitted) */
	CMD_ERASE  = 0x04,	 /* Erase a block of memory         (received)    */
	CMD_GO     = 0x05,	 /* Begin execution at an address   (received)    */
	CMD_NOP    = 0x06,	 /* No operation, for debug         (received)    */
	CMD_PREQ   = 0x07,	 /* Request implementation info     (received)    */
	CMD_PARAMS = 0x08,	 /* Provide implementation info     (transmitted) */
	CMD_DUMP   = 0x09,	 /* Debug: dump a block of memory   (received)    */
	CMD_RESET  = 0x0A,	 /* Reset the phone                 (received)    */
	CMD_UNLOCK = 0x0B,	 /* Unlock access to secured ops    (received)    */
	CMD_VERREQ = 0x0C,	 /* Request software version info   (received)    */
	CMD_VERRSP = 0x0D,	 /* Provide software version info   (transmitted) */
	CMD_PWROFF = 0x0E,	 /* Turn phone power off            (received)    */
	CMD_READ_CODE_FLASH  = 0x80,
	CMD_ERASE_CODE_FLASH = 0x81,
	CMD_WRITE_CODE_FLASH = 0x82,
	CMD_READ_DATA_FLASH  = 0x83,
	CMD_ERASE_DATA_FLASH = 0x84,
	CMD_WRITE_DATA_FLASH = 0x85,
	CMD_CHANGE_BAUD_RATE = 0x90
} cmd_code_type;


/*-------------------------------------------------------------------------*/

/* These packets are the same every time they are transmitted by the phone,
   so we can use pre-computed "canned" messages.  All byte-stuffing required
   by the protocol for transparency has already been done.  Each packet ends
   with a flag, which is both transmitted and used as the termination
   indicator for these arrays. */

static const byte rsp_ack[] =                   /* ACK */
{
	CMD_ACK,			 /* ACK type */
	0x6a,				 /* CRC, MSB */
	0xd3,				 /* CRC, LSB */
	ASYNC_HDLC_FLAG
};

static const byte rsp_nak_invalid_fcs[] =       /* NAK_INVALID_FCS */
{
	CMD_NAK,			 /* NACK type */
	0x00,				 /* Reason code, MSB */
	0x01,				 /* Reason code, LSB */
	0x21,				 /* CRC, MSB */
	0x38,				 /* CRC, LSB */
	ASYNC_HDLC_FLAG
};

static const byte rsp_nak_invalid_dest[] =      /* NAK_INVALID_DEST */
{
	CMD_NAK,			 /* NACK type */
	0x00,				 /* Reason code, MSB */
	0x02,				 /* Reason code, LSB */
	0xba,				 /* CRC, MSB */
	0x0a,				 /* CRC, LSB */
	ASYNC_HDLC_FLAG
};

static const byte rsp_nak_invalid_len[] =       /* NAK_INVALID_LEN */
{
	CMD_NAK,			 /* NACK type */
	0x00,				 /* Reason code, MSB */
	0x03,				 /* Reason code, LSB */
	0x33,				 /* CRC, MSB */
	0x1b,				 /* CRC, LSB */
	ASYNC_HDLC_FLAG
};

static const byte rsp_nak_early_end[] =         /* NAK_EARLY_END */
{
	CMD_NAK,			 /* NACK type */
	0x00,				 /* Reason code, MSB */
	0x04,				 /* Reason code, LSB */
	0x8c,				 /* CRC, MSB */
	0x6f,				 /* CRC, LSB */
	ASYNC_HDLC_FLAG
};

static const byte rsp_nak_too_large[] =         /* NAK_TOO_LARGE */
{
	CMD_NAK,					  /* NACK type */
	0x00,						  /* Reason code, MSB */
	0x05,						  /* Reason code, LSB */
	0x05,						  /* CRC, MSB */
	ASYNC_HDLC_ESC,
	0x7e ^ ASYNC_HDLC_ESC_MASK,	  /* CRC, LSB */
	ASYNC_HDLC_FLAG
};

static const byte rsp_nak_invalid_cmd[] =       /* NAK_INVALID_CMD */
{
	CMD_NAK,			 /* NACK type */
	0x00,				 /* Reason code, MSB */
	0x06,				 /* Reason code, LSB */
	0x9e,				 /* CRC, MSB */
	0x4c,				 /* CRC, LSB */
	ASYNC_HDLC_FLAG
};

static const byte rsp_nak_failed[] =            /* NAK_FAILED */
{
	CMD_NAK,			 /* NACK type */
	0x00,				 /* Reason code, MSB */
	0x07,				 /* Reason code, LSB */
	0x17,				 /* CRC, MSB */
	0x5d,				 /* CRC, LSB */
	ASYNC_HDLC_FLAG
};

static const byte rsp_nak_wrong_iid[] =         /* NAK_WRONG_IID */
{
	CMD_NAK,			 /* NACK type */
	0x00,				 /* Reason code, MSB */
	0x08,				 /* Reason code, LSB */
	0xe0,				 /* CRC, MSB */
	0xa5,				 /* CRC, LSB */
	ASYNC_HDLC_FLAG
};

static const byte rsp_nak_bad_vpp[] =           /* NAK_BAD_VPP */
{
	CMD_NAK,			 /* NACK type */
	0x00,				 /* Reason code, MSB */
	0x09,				 /* Reason code, LSB */
	0x69,				 /* CRC, MSB */
	0xb4,				 /* CRC, LSB */
	ASYNC_HDLC_FLAG
};

static const byte rsp_nak_verify_failed[] =     /* NAK_VERIFY_FAILED */
{
	CMD_NAK,			 /* NACK type */
	0x00,				 /* Reason code, MSB */
	0x0a,				 /* Reason code, LSB */
	0xf2,				 /* CRC, MSB */
	0x86,				 /* CRC, LSB */
	ASYNC_HDLC_FLAG
};

static const byte rsp_nak_no_sec_code[] =          /* NAK_NO_SEC_CODE */
{
	CMD_NAK,	/* NACK type */
	0x000,		/* Reason code, MSB */
	0x00B,		/* Reason code, LSB */
	0x07B,		/* CRC, MSB */
	0x097,		/* CRC, LSB */
	ASYNC_HDLC_FLAG
};

static const byte rsp_nak_bad_sec_code[] =        /* NAK_BAD_SEC_CODE */
{
	CMD_NAK,	/* NACK type */
	0x000,		/* Reason code, MSB */
	0x00C,		/* Reason code, LSB */
	0x0C4,		/* CRC, MSB */
	0x0E3,		/* CRC, LSB */
	ASYNC_HDLC_FLAG
};

static const byte rsp_nak_blk_locked[] =  /* NAK_BLK_LOCKED */
{
	CMD_NAK,	/* NACK type */
	0x000,		/* Reason code, MSB */
	0x00D,		/* Reason code, LSB */
	0x04D,		/* CRC, MSB */
	0x0F2,		/* CRC, LSB */
	ASYNC_HDLC_FLAG
};

static const byte rsp_nak_unlock_failed[] = /*  NAK_UNLOCK_FAILED*/
{
	CMD_NAK,	/* NACK type */
	0x000,		/* Reason code, MSB */
	0x00E,		/* Reason code, LSB */
	0x0D6,		/* CRC, MSB */
	0x0C0,		/* CRC, LSB */
	ASYNC_HDLC_FLAG
};

/*-------------------------------------------------------------------------*/

/* This table translates a response code into a pointer to a canned
   response packet.  The table must be in the same order as the
   response_code_type enum. */

static const byte * const response_table[] =
{
	rsp_ack,
	rsp_nak_invalid_fcs,
	rsp_nak_invalid_dest,
	rsp_nak_invalid_len,
	rsp_nak_early_end,
	rsp_nak_too_large,
	rsp_nak_invalid_cmd,
	rsp_nak_failed,
	rsp_nak_wrong_iid,
	rsp_nak_bad_vpp,
	rsp_nak_verify_failed,
	rsp_nak_no_sec_code,
	rsp_nak_bad_sec_code,
	rsp_nak_blk_locked,
	rsp_nak_unlock_failed
};

/*-------------------------------------------------------------------------*/

/* Other packets must be built up on the fly.  This data type and the
   associated macros support dynamic construction of a packet.  */


typedef PACKED struct
{
	word      length;				  /* Length of packet so far */
	boolean   broken;				  /* Set if the packet can't be built */
	byte      buf[MAX_RESPONSE_LEN];  /* The packet under construction */
} pkt_buffer_type;


#define do_write_code( data, addr, size )  TRUE
#define do_erase_code( addr, size )       TRUE
#define do_write_data( data, addr, size ) TRUE
#define do_erase_data( addr, size )     TRUE

/* Following are added by Fraser.wang */
#define do_flash_write( data, baseaddr, addr, size )  amd_29dl162_do_write( data, baseaddr, addr, size )
#define do_flash_erase( baseaddr, addr, size )        amd_29dl162_do_erase( baseaddr, addr, size )

/* Mask for CRC-16 polynomial:
**
**      x^16 + x^12 + x^5 + 1
**
** This is more commonly referred to as CCITT-16.
** Note:  the x^16 tap is left off, it's implicit.
*/
#define CRC_16_L_POLYNOMIAL     0x8408

/* Seed value for CRC register.  The all ones seed is part of CCITT-16, as
** well as allows detection of an entire data stream of zeroes.
*/
#define CRC_16_L_SEED           0xFFFF

/* Residual CRC value to compare against return value of a CRC_16_L_STEP().
** Use CRC_16_L_STEP() to calculate a 16 bit CRC, complement the result,
** and append it to the buffer.  When CRC_16_L_STEP() is applied to the
** unchanged entire buffer, and complemented, it returns CRC_16_L_OK.
** That is, it returns CRC_16_L_OK_NEG.
*/
#define CRC_16_L_OK             0x0F47
#define CRC_16_L_OK_NEG         0xF0B8

/* The CRC table size is based on how many bits at a time we are going
** to process through the table.  Given that we are processing the data
** 8 bits at a time, this gives us 2^8 (256) entries.
*/
#define CRC_TAB_SIZE    256             /* 2^CRC_TAB_BITS      */

/* CRC table for 16 bit CRC, with generator polynomial 0x8408,
** calculated 8 bits at a time, LSB first.  This table is used
** from a macro in sio.c.
*/
const word crc_16_l_table[ CRC_TAB_SIZE ] = {
  0x0000, 0x1189, 0x2312, 0x329b, 0x4624, 0x57ad, 0x6536, 0x74bf,
  0x8c48, 0x9dc1, 0xaf5a, 0xbed3, 0xca6c, 0xdbe5, 0xe97e, 0xf8f7,
  0x1081, 0x0108, 0x3393, 0x221a, 0x56a5, 0x472c, 0x75b7, 0x643e,
  0x9cc9, 0x8d40, 0xbfdb, 0xae52, 0xdaed, 0xcb64, 0xf9ff, 0xe876,
  0x2102, 0x308b, 0x0210, 0x1399, 0x6726, 0x76af, 0x4434, 0x55bd,
  0xad4a, 0xbcc3, 0x8e58, 0x9fd1, 0xeb6e, 0xfae7, 0xc87c, 0xd9f5,
  0x3183, 0x200a, 0x1291, 0x0318, 0x77a7, 0x662e, 0x54b5, 0x453c,
  0xbdcb, 0xac42, 0x9ed9, 0x8f50, 0xfbef, 0xea66, 0xd8fd, 0xc974,
  0x4204, 0x538d, 0x6116, 0x709f, 0x0420, 0x15a9, 0x2732, 0x36bb,
  0xce4c, 0xdfc5, 0xed5e, 0xfcd7, 0x8868, 0x99e1, 0xab7a, 0xbaf3,
  0x5285, 0x430c, 0x7197, 0x601e, 0x14a1, 0x0528, 0x37b3, 0x263a,
  0xdecd, 0xcf44, 0xfddf, 0xec56, 0x98e9, 0x8960, 0xbbfb, 0xaa72,
  0x6306, 0x728f, 0x4014, 0x519d, 0x2522, 0x34ab, 0x0630, 0x17b9,
  0xef4e, 0xfec7, 0xcc5c, 0xddd5, 0xa96a, 0xb8e3, 0x8a78, 0x9bf1,
  0x7387, 0x620e, 0x5095, 0x411c, 0x35a3, 0x242a, 0x16b1, 0x0738,
  0xffcf, 0xee46, 0xdcdd, 0xcd54, 0xb9eb, 0xa862, 0x9af9, 0x8b70,
  0x8408, 0x9581, 0xa71a, 0xb693, 0xc22c, 0xd3a5, 0xe13e, 0xf0b7,
  0x0840, 0x19c9, 0x2b52, 0x3adb, 0x4e64, 0x5fed, 0x6d76, 0x7cff,
  0x9489, 0x8500, 0xb79b, 0xa612, 0xd2ad, 0xc324, 0xf1bf, 0xe036,
  0x18c1, 0x0948, 0x3bd3, 0x2a5a, 0x5ee5, 0x4f6c, 0x7df7, 0x6c7e,
  0xa50a, 0xb483, 0x8618, 0x9791, 0xe32e, 0xf2a7, 0xc03c, 0xd1b5,
  0x2942, 0x38cb, 0x0a50, 0x1bd9, 0x6f66, 0x7eef, 0x4c74, 0x5dfd,
  0xb58b, 0xa402, 0x9699, 0x8710, 0xf3af, 0xe226, 0xd0bd, 0xc134,
  0x39c3, 0x284a, 0x1ad1, 0x0b58, 0x7fe7, 0x6e6e, 0x5cf5, 0x4d7c,
  0xc60c, 0xd785, 0xe51e, 0xf497, 0x8028, 0x91a1, 0xa33a, 0xb2b3,
  0x4a44, 0x5bcd, 0x6956, 0x78df, 0x0c60, 0x1de9, 0x2f72, 0x3efb,
  0xd68d, 0xc704, 0xf59f, 0xe416, 0x90a9, 0x8120, 0xb3bb, 0xa232,
  0x5ac5, 0x4b4c, 0x79d7, 0x685e, 0x1ce1, 0x0d68, 0x3ff3, 0x2e7a,
  0xe70e, 0xf687, 0xc41c, 0xd595, 0xa12a, 0xb0a3, 0x8238, 0x93b1,
  0x6b46, 0x7acf, 0x4854, 0x59dd, 0x2d62, 0x3ceb, 0x0e70, 0x1ff9,
  0xf78f, 0xe606, 0xd49d, 0xc514, 0xb1ab, 0xa022, 0x92b9, 0x8330,
  0x7bc7, 0x6a4e, 0x58d5, 0x495c, 0x3de3, 0x2c6a, 0x1ef1, 0x0f78
};

#define CRC_16_L_STEP(xx_crc,xx_c) \
  (((xx_crc) >> 8) ^ crc_16_l_table[((xx_crc) ^ (xx_c)) & 0x00ff])

typedef struct {
  const  dword   out_addr;    /* GPIO output address */
  const  dword   in_addr;     /* GPIO input address */
  const  dword   tsen_addr;   /* GPIO tristate control port address */
  word           out_buf;     /* GPIO OUT  port shadow buffer */
  word           ts_buf;      /* GPIO TSEN port shadow buffer */
} bio_gpio_info_type;

/*===========================================================================

MACRO BIO_OUT

DESCRIPTION
  This macro outputs a specified value, qualified by a specified mask, to a
  specified port.  Only the bits corresponding to the mask are actually
  affected; other bits retain their previous values.  To do this, an image is
  maintained of the previous value written to the port which is combined with
  the new value prior to writing.

PARAMETERS
  io    Basic I/O port defined by this header

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?