summaryrefslogtreecommitdiff
path: root/cleopatre/u-boot-1.1.6/test/spidupd_test/spidupd_test.c
diff options
context:
space:
mode:
Diffstat (limited to 'cleopatre/u-boot-1.1.6/test/spidupd_test/spidupd_test.c')
-rw-r--r--cleopatre/u-boot-1.1.6/test/spidupd_test/spidupd_test.c275
1 files changed, 216 insertions, 59 deletions
diff --git a/cleopatre/u-boot-1.1.6/test/spidupd_test/spidupd_test.c b/cleopatre/u-boot-1.1.6/test/spidupd_test/spidupd_test.c
index 0e10344c66..b7505ced19 100644
--- a/cleopatre/u-boot-1.1.6/test/spidupd_test/spidupd_test.c
+++ b/cleopatre/u-boot-1.1.6/test/spidupd_test/spidupd_test.c
@@ -41,16 +41,15 @@
#define SRC_ETHER_ADDR "00:11:95:dc:d1:3f"
#define DST_ETHER_ADDR "00:50:c2:38:00:0B"
-#define PROT_SPIDUPD 0x88E1 /* MME Ethertype (SPiDCOM Update) */
-#define RCV_TIMEOUT 5 /* receive timeout in seconds */
+#define PROT_SPIDUPD 0x88E1 /* MME Ethertype (SPiDCOM Update) */
+#define SRV_CNF_TIMEOUT 5 /* server response timeout in seconds */
+#define SRV_TIMEOUT_ERR_CODE -111 /* arbitrary chosen error code for server timeout event */
+
+#define MAX_TMO_RETRY 5 /* Number of retries in case of server response timeout */
+#define MAX_TMO_END_RETRY 120 /* Wait a little longer for UPDATE_END_CNF, because it takes time to write flash */
+#define MAX_PKT_RETRY 5 /* Number of retries in case of server not ack the package */
+
-/* SPIDUPD message primitives */
-static VsUpdStartReq_t *vs_update_start_req;
-static VsUpdStartCnf_t *vs_update_start_cnf;
-static VsUpdTransfReq_t *vs_update_transfer_req;
-static VsUpdTransfCnf_t *vs_update_transfer_cnf;
-static VsUpdEndReq_t *vs_update_end_req;
-static VsUpdEndCnf_t *vs_update_end_cnf;
static void sprintf_hex(unsigned char * str, const unsigned char * ptr,
int len, unsigned char delim)
@@ -86,9 +85,7 @@ int GetMacAddr (char *device, int rawsock, unsigned char our_addr[6])
for( i = 0; i < 6; i++ )
{
our_addr[i] = (unsigned char)ifr.ifr_hwaddr.sa_data[i];
- printf("%.2x ", our_addr[i]);
}
- printf("\n");
return 0;
}
@@ -141,7 +138,6 @@ int BindRawSocketToInterface(char *device, int rawsock, int protocol)
int SendRawPacket(int rawsock, unsigned char *pkt, int pkt_len)
{
int sent= 0;
- printf("Packet len: %d\n", pkt_len);
/* A simple write on the socket ..thats all it takes ! */
if((sent = write(rawsock, pkt, pkt_len)) != pkt_len)
@@ -235,31 +231,80 @@ int ListenSocket(int raw)
int len;
struct sockaddr_ll packet_info;
int packet_info_size = sizeof(packet_info);
- MME_t s;
+ MME_t *s;
+ int res;
+
+ fd_set readset;
+ struct timeval srv_cnf_timeout;
+
+ int rc;
+
+ VsUpdStartCnf_t *vs_update_start_cnf;
+ VsUpdTransfCnf_t *vs_update_transfer_cnf;
+ VsUpdEndCnf_t *vs_update_end_cnf;
packet_buffer = (unsigned char *)calloc(PKTSIZE, sizeof(char));
pkt_start= packet_buffer;
- /* Set socket receive timeout */
- SetSocketTimeout(raw, RCV_TIMEOUT*1000);
+ /*
+ * check for socket timeout
+ */
+ FD_ZERO(&readset);
+ FD_SET(raw, &readset);
+ srv_cnf_timeout.tv_sec = SRV_CNF_TIMEOUT;
+ srv_cnf_timeout.tv_usec = 0;
+
+ /* readset fd_set is containing only our socket, so no need to check with FD_ISSET() */
+ res = select(raw + 1, &readset, NULL, NULL, &srv_cnf_timeout);
+ if (res < 0)
+ {
+ perror("select() failed");
+ }
+ else if (res == 0) /* timeout */
+ {
+ //printf("Server response timeout.\n");
+ rc = SRV_TIMEOUT_ERR_CODE;
+ }
+ else /* everything is cool, socket is ready for reading */
+ {
- if( ( len = recvfrom(raw, packet_buffer, PKTSIZE, 0, (struct sockaddr*)&packet_info, &packet_info_size) ) < 0 )
- {
- perror("Recv from returned error: ");
- exit(-1);
- }
- else
- {
- /* Packet has been received successfully !! */
- PrintPacketInHex(packet_buffer, len);
- }
+ if( ( len = recvfrom(raw, packet_buffer, PKTSIZE, 0, (struct sockaddr*)&packet_info, &packet_info_size) ) < 0 )
+ {
+ perror("Recv from returned error: ");
+ exit(-1);
+ }
+ else
+ {
+ /* Packet has been received successfully !! */
+ //PrintPacketInHex(packet_buffer, len);
+ }
- //packet_buffer += MME_HDR_SIZE;
- packet_buffer += sizeof(s);
+ s = (MME_t *)packet_buffer;
+ packet_buffer += MME_HDR_SIZE;
- vs_update_start_cnf = (VsUpdStartCnf_t *)packet_buffer;
+ switch ( mmetohs(s->mmtype) )
+ {
+ case VS_UPDATE_START_CNF :
+ vs_update_start_cnf = (VsUpdStartCnf_t *)packet_buffer;
+ rc = vs_update_start_cnf->start_update;
+ break;
+
+ case VS_UPDATE_TRANSFER_CNF :
+ vs_update_transfer_cnf = (VsUpdTransfCnf_t *)packet_buffer;
+ rc = vs_update_transfer_cnf->ack;
+ break;
+
+ case VS_UPDATE_END_CNF :
+ vs_update_end_cnf = (VsUpdEndCnf_t *)packet_buffer;
+ rc = vs_update_end_cnf->result;
+ break;
+
+ deafault :
+ break;
+ }
+ }
- return vs_update_start_cnf->start_update;
+ return rc;
}
/* argv[1] is the device e.g. eth0 */
@@ -283,6 +328,17 @@ main(int argc, char **argv)
MD5_CTX ctx;
int read_bytes;
int i;
+ int pkt_retry = 0;
+ int tmo_retry = 0;
+ int res;
+
+ static VsUpdStartReq_t *vs_update_start_req;
+ static VsUpdTransfReq_t *vs_update_transfer_req;
+ static VsUpdEndReq_t *vs_update_end_req;
+
+
+
+
/* Create the raw socket */
raw = CreateRawSocket(ETH_P_ALL);
@@ -329,36 +385,63 @@ main(int argc, char **argv)
/*Detertermine total packet length */
pkt_len = packet - pkt_start;
- /* Position packet pointer to strt of tyhe packet,
- and send the packet */
+ /* Position packet pointer to strt of the packet */
packet = pkt_start;
- if(!SendRawPacket(raw, packet, pkt_len))
- {
- perror("Error sending packet");
- }
- else
- printf("Packet sent successfully\n");
- if ( ListenSocket(raw) != SPIDUPD_SUCESS )
+
+ /* if packet is not acknowledged ot it is timed out,
+ * try several times to re-send same packet */
+ printf("Requesting update start... ");
+ fflush(stdout);
+ tmo_retry = MAX_TMO_RETRY;
+ while (tmo_retry > 0)
{
- perror("Cannot start update.");
+ if(!SendRawPacket(raw, packet, pkt_len))
+ {
+ perror("Error sending packet");
+ }
+ else
+ //printf("Requested update start...");
+
+ if ( ( res = ListenSocket(raw) ) == SRV_TIMEOUT_ERR_CODE )
+ {
+ tmo_retry--;
+ continue;
+ }
+ else if (res != SPIDUPD_SUCCESS)
+ {
+ printf("Error - Cannot start update.\n");
+ exit(-1);
+ }
+ else /* success */
+ {
+ printf("OK\n");
+ break;
+ }
+ }
+
+ if (tmo_retry == 0)
+ {
+ printf("\nTimeout error : no response from the server.\n");
+ exit(-1);
}
/*
* VS_UPDATE_TRANSFER_REQ
*/
- //pImgFile = fopen("linux-kernel-2.6.25.10-arm", "r");
- pImgFile = fopen("test.bin", "r");
+ pImgFile = fopen("linux-kernel-2.6.25.10-arm", "r");
+ //pImgFile = fopen("test.bin", "r");
read_bytes = fread (tmp_buffer, 1 , SPIDUPD_BLOCK_SIZE, pImgFile);
/* init md5 context before we start calculating md5_sum
* we will need md5 sum to send in the end */
MD5_Init(&ctx);
+ printf("Uploading Linux image :\n");
+
do
{
-
/* We are about to send new block */
block_counter++;
@@ -392,24 +475,68 @@ main(int argc, char **argv)
/*Detertermine total packet length */
pkt_len = packet - pkt_start;
- /* Position packet pointer to strt of tyhe packet,
- and send the packet */
+ /* Position packet pointer to strt of the packet */
packet = pkt_start;
- if(!SendRawPacket(raw, packet, pkt_len))
+
+ /* if packet is not acknowledged ot it is timed out,
+ * try several times to re-send same packet */
+ tmo_retry = MAX_TMO_RETRY;
+ pkt_retry = MAX_PKT_RETRY;
+ while ( (tmo_retry > 0) && (pkt_retry > 0) )
{
- perror("Error sending packet");
+ if(!SendRawPacket(raw, packet, pkt_len))
+ {
+ perror("Error sending packet");
+ }
+ else
+ {
+ if ( (block_counter % 50) == 0 )
+ {
+ printf("#");
+ fflush(stdout);
+ }
+
+ if ( (block_counter % 2500) == 0 )
+ {
+ putchar('\n');
+ fflush(stdout);
+ }
+ }
+
+ if ( ( res = ListenSocket(raw) ) == SRV_TIMEOUT_ERR_CODE )
+ {
+ tmo_retry--;
+ continue;
+ }
+ else if (res != SPIDUPD_SUCCESS)
+ {
+ printf("Packet not acknowledged. ");
+
+ pkt_retry--;
+ continue;
+ }
+ else /* success */
+ {
+ break;
+ }
+ }
+
+ if (tmo_retry == 0)
+ {
+ printf("timeout error : no response from the server.\n");
+ exit(-1);
}
- else
- printf("Packet sent successfully\n");
-
- if ( ListenSocket(raw) != SPIDUPD_SUCESS )
+ if (pkt_retry == 0)
{
- perror("Cannot do transfer.");
+ printf("Maximum number of packet retries exceeded.\n");
+ exit(-1);
}
} while ( (read_bytes = fread (tmp_buffer, 1 , SPIDUPD_BLOCK_SIZE, pImgFile) ) > 0 );
+ printf("\nDone.\n");
+
/* Get final md5 */
MD5_Final(md5_sum, &ctx);
@@ -445,18 +572,48 @@ main(int argc, char **argv)
and send the packet */
packet = pkt_start;
- if(!SendRawPacket(raw, packet, pkt_len))
- {
- perror("Error sending packet");
- }
- else
- printf("Packet sent successfully\n");
+ /* if packet is not timed-out try several times to re-send same packet */
+ printf("Waiting on the server to burn image on flash :\n");
+ fflush(stdout);
+ tmo_retry = MAX_TMO_END_RETRY;
+ while (tmo_retry > 0)
+ {
+ if(!SendRawPacket(raw, packet, pkt_len))
+ {
+ perror("Error sending packet");
+ }
+ else
+ //printf("UPD_END_REQ sent : ");
- if ( ListenSocket(raw) != SPIDUPD_SUCESS )
+
+ if ( ( res = ListenSocket(raw) ) == SRV_TIMEOUT_ERR_CODE )
+ {
+ printf(">");
+ fflush(stdout);
+ tmo_retry--;
+ continue;
+ }
+ else if (res != SPIDUPD_SUCCESS)
+ {
+ printf("Error - Cannot finish update.\n");
+ exit(-1);
+ }
+ else /* success */
+ {
+ printf("| Done.\n");
+ break;
+ }
+ }
+
+ if (tmo_retry == 0)
{
- perror("Cannot end update.");
+ printf("Timeout error : no response from the server.\n");
+ exit(-1);
}
+ /* if we came this far, image was well updated */
+ printf("\nImage has been sucesfully updated.\n\n");
+
/* --- END OF MESSAGES --- */
/* Free the ethernet_header back to the heavenly heap */