www.pudn.com > at91rm9200vxworksbsp.rar > ftpcs.c
/* ftpcs.c */
/* Copyright 1984 - 2003 Grentech, Inc. */
/*
modification history
--------------------
*/
/*
DESCRIPTION
*/
/* Included files */
#include "vxWorks.h"
#include "ftpLib.h"
#include "stdio.h"
#include "string.h"
#include "taskLib.h"
#include "tftpLib.h"
#include "config.h"
#include "iolib.h"
#include "fiolib.h"
#include "ctype.h"
/* Definitions */
#define FTP_HOST_IP_LENGTH 50
#define FTP_FILE_NAME_LENGTH 50
#define FTP_USR_NAME_LENGTH 50
#define FTP_PASSWORDS_LENGTH 50
#define FTP_ACCOUNT_LENGTH 50
/* Local Function Declarations */
LOCAL void ftp_parameters_init(void);
#if 0
LOCAL STATUS ftp_get(char *pString);
#endif
LOCAL STATUS ftp_open(char *);
LOCAL STATUS ftp_disconnect(char *) ;
LOCAL STATUS ftp_quit(char *);
LOCAL STATUS ftp_help(char *);
LOCAL BOOL execute_ftp_cmd(char *);
LOCAL BOOL ftp_cmd_loop(void);
LOCAL STATUS tftp_get(char *,char *, UINT32 *);
/* Extern Function Declarations */
extern STATUS fs_write_datum_to_file(char *, char *, UINT32);
extern STATUS read_datum_from_fd(int, char *, UINT32 *);
#ifdef INCLUDE_NOR_FLASH
extern int write_nor_flash (unsigned int addr_dest, unsigned int addr_src, unsigned int size);
#endif
#ifdef INCLUDE_SPI_DATAFLASH
extern int AT91F_DataflashInit (void);
extern int write_dataflash (unsigned long addr_dest, unsigned int addr_src,unsigned int size);
#endif /*INCLUDE_SPI_DATAFLASH*/
typedef struct
{
char *cmd_string;
STATUS (*pcmd_function)(char *);
char *helpinfo;
}FTP_CMDS;
FTP_CMDS ftp_command[] =
{
{"open", ftp_open, ""},
{"get", ftp_open, ""},
{"disconnect", ftp_disconnect, ""},
{"quit", ftp_quit, ""},
{"help", ftp_help, ""},
};
unsigned int run_ftp_cmd_loop = 0;
int ftp_control_socket = ERROR, ftp_data_socket = ERROR;
unsigned char ftp_server_ip[FTP_HOST_IP_LENGTH];
unsigned char ftp_user[FTP_USR_NAME_LENGTH];
unsigned char ftp_pwd[FTP_PASSWORDS_LENGTH];
unsigned char ftp_account[FTP_ACCOUNT_LENGTH];
/*2M bytes buffer for download*/
#define MaxSizeInBytes 0x400000
LOCAL UINT8 download_buffer[MaxSizeInBytes];
/*-----------------------------------------------------------------------------
Function: ftp_auto_get
Overview:
Input: None
Output: None
Return: None
-----------------------------------------------------------------------------*/
LOCAL STATUS ftp_auto_get(char *host,
char *fileName,
char *usr,
char *pwd,
UINT32 *file_length)
{
UINT32 dataFd =0;
UINT32 ctrlFd = 0;
STATUS status = OK;
char buffer[2048];
taskLock();
memset(buffer, 0, sizeof(buffer));
printf("\r\nDownloading \"%s\" from %s...", fileName, host);
if (ftpXfer (host, usr, pwd, "", "RETR %s", "", fileName, &ctrlFd, &dataFd) == ERROR)
{
/*num = read(ctrlFd, buffer, sizeof(buffer));*/
printf("\r\nCan't establish link to host %s.\r\n", host);
return (ERROR);
}
if (status == OK)
{
#if 0
char *current_pointer = NULL;
size = 0;
current_pointer = download_buffer;
while ((num = read(dataFd, buffer, sizeof(buffer))) > 0)
{
size += num;
if (size > sizeof(download_buffer))
{
printf("Invalid file size!\r\n");
break;
}
memcpy(current_pointer, buffer, num);
#if 0
memset(buffer, 0, sizeof(buffer));
#endif
current_pointer += num;
printf(".");
}
#else
if (read_datum_from_fd(dataFd, download_buffer, file_length) != OK)
{
printf("\nread_datum_from_fd error");
status = ERROR;
}
#endif
close(dataFd);
#if 0
if (num < 0) /* read error? */
{
printf("read error.\n");
status = ERROR;
}
if (ftpReplyGet(ctrlFd, TRUE) != FTP_COMPLETE)
{
printf("get reply error.\n");
status = ERROR;
}
#endif
if (ftpCommand (ctrlFd, "QUIT", 0, 0, 0, 0, 0, 0) != FTP_COMPLETE)
{
printf("quit error.\n");
status = ERROR;
}
close (ctrlFd);
}
if (status == OK)
{
printf("done.%d bytes reserved.\n", *file_length);
}
taskUnlock();
return status;
}
/*-----------------------------------------------------------------------------
Function: ftp_parameters_init
Overview:
Input: None
Output: None
Return: None
-----------------------------------------------------------------------------*/
LOCAL void ftp_parameters_init(void)
{
memset(ftp_server_ip, '\0', sizeof(ftp_server_ip));
memset(ftp_user, '\0', sizeof(ftp_user));
memset(ftp_pwd, '\0', sizeof(ftp_pwd));
memset(ftp_account, '\0', sizeof(ftp_account));
ftp_control_socket = ERROR;
ftp_data_socket = ERROR;
}
#if 0
/*-----------------------------------------------------------------------------
Function: ftp_get
Overview:
Input: None
Output: None
Return: None
-----------------------------------------------------------------------------*/
LOCAL STATUS ftp_get(char *pString)
{
#if 0
UINT8 file_name[FTP_FILE_NAME_LENGTH];
UINT8 abs_file_name[FTP_FILE_NAME_LENGTH];
UINT32 file_length = 0;
if (ftp_control_socket != ERROR)
{
printf("Not connected.\r\n");
return ERROR;
}
memset(file_name, '\0', sizeof(file_name));
memset(abs_file_name, '\0', sizeof(abs_file_name));
switch (sscanf(pString, "%*s %s %s %s %s", file_name))
{
case EOF:
printf("Remote file :");
scanf("%s", file_name);
break;
case 0:
break;
default:
}
strncpy(abs_file_name, DEFAULT_FS_BOOT_MOUNT_POINT, strlen(DEFAULT_FS_BOOT_MOUNT_POINT));
strcat(abs_file_name, file_name);
if (read_datum_from_fd(ftp_data_socket, download_buffer, file_length) != OK)
{
printf("Get %s failed.\r\n", );
return ERROR;
}
if (fs_write_datum_to_file(abs_file_name, download_buffer, file_length) != OK)
{
printf("Save %s failed.\r\n", abs_file_name);
return ERROR;
}
#endif
return OK;
}
#endif
/*-----------------------------------------------------------------------------
Function: ftp_open
Overview:
Input: None
Output: None
Return: None
-----------------------------------------------------------------------------*/
LOCAL STATUS ftp_open(char *pString)
{
register int n;
if (ftp_control_socket != ERROR)
{
printf("Already connected to %s, use disconnect first.\r\n", ftp_server_ip);
return ERROR;
}
if (sscanf(pString, "%*s %s %s %s", ftp_server_ip, ftp_user, ftp_pwd) == EOF)
{
return ERROR;
}
if ((ftp_control_socket = ftpHookup(ftp_server_ip)) == ERROR)
{
return ERROR;
}
printf("Connected to %s.\n", ftp_server_ip);
if (strlen(ftp_user) == 0)
{
printf("ready for new user.\r\nUser <%s>:", ftp_server_ip);
scanf("%s", ftp_user);
}
n = ftpCommand (ftp_control_socket, "USER %s", (int)ftp_user, 0, 0, 0, 0, 0);
if (n == FTP_CONTINUE)
{
if (strlen(ftp_pwd) == 0)
{
printf("Give me your password, please.\r\nPassword:");
#if 0
(void) ioctl (STD_IN, FIOSETOPTIONS,
OPT_CRMOD | OPT_TANDEM | OPT_7_BIT);
#endif
scanf("%s", ftp_pwd);
}
n = ftpCommand (ftp_control_socket, "PASS %s", (int)ftp_pwd, 0, 0, 0, 0, 0);
}
if (n == FTP_CONTINUE)
n = ftpCommand (ftp_control_socket, "ACCT %s", 0, 0, 0, 0, 0, 0);
if (n != FTP_COMPLETE)
{
return ERROR;
}
printf("Logged in successfully.\n");
return OK;
}
/*-----------------------------------------------------------------------------
Function: ftp_disconnect
Overview:
Input: None
Output: None
Return: None
-----------------------------------------------------------------------------*/
LOCAL STATUS ftp_disconnect(char *pString)
{
if (ftp_control_socket != ERROR)
{
printf("Disconnect to %s.\n", ftp_server_ip);
ftpCommand (ftp_control_socket, "QUIT",0,0,0,0,0,0);
close(ftp_control_socket);
close(ftp_data_socket);
ftp_parameters_init();
}
else
{
printf("Not connected.\r\n");
}
return OK;
}
/*-----------------------------------------------------------------------------
Function: ftp_quit
Overview:
Input: None
Output: None
Return: None
-----------------------------------------------------------------------------*/
LOCAL STATUS ftp_quit(char *pString)
{
if (ftp_control_socket != ERROR)
{
ftpCommand (ftp_control_socket, "QUIT",0,0,0,0,0,0);
close(ftp_control_socket);
close(ftp_data_socket);
ftp_parameters_init();
}
run_ftp_cmd_loop = 0;
return OK;
}
/*-----------------------------------------------------------------------------
Function: ftp_quit
Overview:
Input: None
Output: None
Return: None
-----------------------------------------------------------------------------*/
LOCAL STATUS ftp_help(char *pString)
{
unsigned int loop = 0;
printf("Commands may be abbreviated. Commands are:\r\n\r\n");
for(loop =0; loop < sizeof(ftp_command)/sizeof(FTP_CMDS); loop++)
{
if (((loop % 5) == 0) && (loop != 0))
{
printf("\r\n");
}
printf("%-12s", ftp_command[loop].cmd_string);
}
printf("\r\n");
return OK;
}
/*-----------------------------------------------------------------------------
Function: execute_ftp_cmd
Overview:
Input: pLine
Output: None
Return: None
-----------------------------------------------------------------------------*/
LOCAL BOOL execute_ftp_cmd(char *pLine)
{
UINT32 cmd_menbers = 0;
UINT32 max_cmd_menber = sizeof(ftp_command)/sizeof(FTP_CMDS);
UINT32 cmd_length = 0;
/* Now, pull up the corresponding command from the command pools and do it */
for (cmd_menbers = 0; cmd_menbers < max_cmd_menber; cmd_menbers++)
{
cmd_length = strlen(ftp_command[cmd_menbers].cmd_string);
if (strncmp(pLine, ftp_command[cmd_menbers].cmd_string, cmd_length) == 0)
{
ftp_command[cmd_menbers].pcmd_function(pLine);
return TRUE;
}
}
return FALSE;
}
#if 0
LOCAL void skipSpace
(
FAST char **strptr /* pointer to pointer to string */
)
{
while (isspace ((UINT) ** strptr))
++ * strptr;
}
#endif
/*-----------------------------------------------------------------------------
Function: ftp_cmd_loop
Overview:
Input: pLine
Output: None
Return: None
-----------------------------------------------------------------------------*/
LOCAL BOOL ftp_cmd_loop(void)
{
char ftp_cmd_line[100];
char *pFtp_cmd_line = (void *)0;
ftp_parameters_init();
run_ftp_cmd_loop = 1;
for(; run_ftp_cmd_loop == 1; )
{
printf ("ftp>");
fioRdString (STD_IN, ftp_cmd_line, sizeof (ftp_cmd_line));
/* take blanks off end of line */
pFtp_cmd_line = ftp_cmd_line + strlen (ftp_cmd_line) - 1; /* point at last char */
while ((pFtp_cmd_line >= ftp_cmd_line) && (*pFtp_cmd_line == ' '))
{
*pFtp_cmd_line = '\0';
pFtp_cmd_line--;
}
pFtp_cmd_line = ftp_cmd_line;
#if 1
while (isspace ((UINT) (*pFtp_cmd_line)))
{
++pFtp_cmd_line;
}
#else
skipSpace(&pFtp_cmd_line);
#endif
if (execute_ftp_cmd(pFtp_cmd_line) != TRUE)
{
switch (*(pFtp_cmd_line++))
{
case '\0': /* blank line */
break;
default:
printf("Invalid command.\r\n");
}
}
}
return OK;
}
/*-----------------------------------------------------------------------------
Function: ftp_cmd_down_file
Overview:
Input: pLine
Output: None
Return: None
-----------------------------------------------------------------------------*/
STATUS ftp_down_file(char *host, char *fileName, char *usr, char *pwd)
{
char abs_file_name[50];
UINT32 file_length = 0;
STATUS status = OK;
#if 1
/* Download the file form remote ftp server firstly */
if (ftp_auto_get(host, fileName, usr, pwd, &file_length) != OK)
{
printf("\nftp_auto_get error");
return ERROR;
}
memset(abs_file_name, '\0', sizeof(char) * sizeof(abs_file_name));
if (strcmp(fileName, DEFAULT_BOOT_FILE) == 0)
{
#ifdef INCLUDE_NOR_FLASH
/* Bootloader are burn into the zero address of flash using driver */
status = (write_nor_flash(0x10000000,
(unsigned int)download_buffer,
file_length) == 0x01) ? OK : ERROR;
#endif /*INCLUDE_NOR_FLASH*/
#ifdef INCLUDE_SPI_DATAFLASH /*DEBUG_END_TRACE*/
AT91F_DataflashInit ();
status = (write_dataflash(0xC0008000, (unsigned int)download_buffer, file_length) == 0x01) ? OK : ERROR;
#endif /*INCLUDE_SPI_DATAFLASH*/
}
else
{
/* Other file are burn into tffs or other area in flash */
#ifdef INCLUDE_TFFS
strncpy(abs_file_name, DEFAULT_FS_BOOT_MOUNT_POINT, strlen(DEFAULT_FS_BOOT_MOUNT_POINT));
strcat(abs_file_name, fileName);
status = (fs_write_datum_to_file(abs_file_name, download_buffer, file_length) == OK) ? OK : ERROR;
#endif
}
if (status == OK)
printf("Update %s succefully.\r\n", fileName);
else
printf("Update %s failed.\r\n", fileName);
return status;
#else
memset(abs_file_name, '\0', sizeof(char) * sizeof(abs_file_name));
#ifdef INCLUDE_TFFS
strncpy(abs_file_name, DEFAULT_FS_BOOT_MOUNT_POINT, strlen(DEFAULT_FS_BOOT_MOUNT_POINT));
#endif
strcat(abs_file_name, fileName);
if (ftp_auto_get(host, fileName, usr, pwd, &file_length) == OK)
{
if (strcmp(fileName, DEFAULT_BOOT_FILE) == 0)
{
if (write_nor_flash(0x10000000, download_buffer, file_length) == 0x01)
printf("update %s successfully.\r\n");
}
else
{
#ifdef INCLUDE_TFFS
if (fs_write_datum_to_file(abs_file_name, download_buffer, file_length) != OK)
{
return ERROR;
}
#endif
}
return OK;
}
else
{
return ERROR;
}
#endif
}
STATUS FtpDownFile(char *host, char *fileName, char *usr, char *pwd)
{
char abs_file_name[50];
UINT32 file_length = 0;
memset(abs_file_name, '\0', sizeof(char) * sizeof(abs_file_name));
#ifdef INCLUDE_TFFS
/*strncpy(abs_file_name, DEFAULT_FS_BOOT_MOUNT_POINT, strlen(DEFAULT_FS_BOOT_MOUNT_POINT));*/
#endif
/*strcat(abs_file_name, fileName);*/
strncpy(abs_file_name, fileName, strlen(fileName));
if (ftp_auto_get(host, fileName, usr, pwd, &file_length) == OK)
{
#ifdef INCLUDE_TFFS
if (fs_write_datum_to_file(abs_file_name, download_buffer, file_length) != OK)
{
return ERROR;
}
#endif
}
else
{
return ERROR;
}
return OK;
}
/*-----------------------------------------------------------------------------
Function: ftp_cmd
Overview:
Input: None
Output: None
Return: None
-----------------------------------------------------------------------------*/
STATUS ftp_cmd(char *pString)
{
UINT8 host_ip[FTP_HOST_IP_LENGTH];
UINT8 file_name[FTP_FILE_NAME_LENGTH];
UINT8 usr[FTP_USR_NAME_LENGTH];
UINT8 passwd[FTP_PASSWORDS_LENGTH];
memset(file_name, '\0', sizeof(file_name));
memset(usr, '\0', sizeof(usr));
memset(passwd, '\0', sizeof(passwd));
switch (sscanf(pString, "%*s %s %s %s %s", host_ip, file_name, usr, passwd))
{
case EOF:
ftp_cmd_loop();
break;
case 0:
break;
default:
if (ftp_down_file(host_ip, file_name, usr, passwd) != OK)
{
return ERROR;
}
}
return OK;
}
/*-----------------------------------------------------------------------------
Function: tftp_get
Overview:
Input: host -- tftp host ip address
file_name -- file name to be downloaded
Output: file_buffer -- point to the head of buffer save the file
file_length -- the length of file
Return: None
-----------------------------------------------------------------------------*/
LOCAL STATUS tftp_get(char *host,char *file_name, UINT32 *file_length)
{
STATUS status = OK;
int dataFd = 0;
int errorFd = 0;
char buffer[2048];
char *current_pointer = NULL;
UINT32 size = 0;
UINT32 num = 0;
taskLock();
memset(buffer, 0, sizeof(buffer));
if (status == OK)
{
printf("Downloading \"%s\" from %s...", file_name, host);
if (tftpXfer(host, 0, file_name,
"get", "binary",
&dataFd, &errorFd) == ERROR)
{
printf("fail.\ncann't connect to %s\r\n",host);
status = ERROR;
}
else
{
printf("done.\n");
}
}
if (status == OK)
{
size = 0;
current_pointer = download_buffer;
while ((num = read(dataFd, buffer, sizeof(buffer))) > 0)
{
size += num;
if (size > sizeof(download_buffer))
{
printf("Invalid file size!\r\n");
break;
}
memcpy(current_pointer, buffer, num);
memset(buffer, 0, sizeof(buffer));
current_pointer += num;
printf(".");
}
close(dataFd);
if (status == OK)
{
num = read(errorFd, buffer, sizeof(buffer));
buffer[num] = '\0';
if (num > 0)
{
printf ("An error occurred!:%s\n", buffer);
status = ERROR;
}
}
close(errorFd);
}
if (status == OK)
{
*file_length = size;
}
taskUnlock();
return status;
}
/*-----------------------------------------------------------------------------
Function: tftp_cmd
Overview:
Input: None
Output: None
Return: None
-----------------------------------------------------------------------------*/
STATUS tftp_cmd(char *pString)
{
UINT8 host_ip[50];
UINT8 file_name[50];
UINT8 usr[50];
UINT8 passwd[50];
UINT32 file_length = 0;
char abs_file_name[50];
memset(abs_file_name, '\0', sizeof(char) * sizeof(abs_file_name));
memset(file_name, '\0', sizeof(file_name));
memset(usr, '\0', sizeof(usr));
memset(passwd, '\0', sizeof(passwd));
if (sscanf(pString, "%*s %s %s %s %s", host_ip, file_name, usr, passwd) == EOF)
{
return ERROR;
}
#ifdef INCLUDE_TFFS
strncpy(abs_file_name, DEFAULT_FS_BOOT_MOUNT_POINT, strlen(DEFAULT_FS_BOOT_MOUNT_POINT));
#endif
strcat(abs_file_name, file_name);
if (tftp_get(host_ip, file_name, &file_length) == OK)
{
#ifdef INCLUDE_TFFS
if (fs_write_datum_to_file(abs_file_name, download_buffer, file_length) != OK)
{
return ERROR;
}
#endif
}
else
{
return ERROR;
}
return OK;
}