-
Notifications
You must be signed in to change notification settings - Fork 0
/
bl_enet.c
1546 lines (1370 loc) · 41.9 KB
/
bl_enet.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//*****************************************************************************
//
// bl_enet.c - Functions to update via Ethernet.
//
// Copyright (c) 2007-2013 Texas Instruments Incorporated. All rights reserved.
// Software License Agreement
//
// Texas Instruments (TI) is supplying this software for use solely and
// exclusively on TI's microcontroller products. The software is owned by
// TI and/or its suppliers, and is protected under applicable copyright
// laws. You may not combine this software with "viral" open-source
// software in order to form a larger program.
//
// THIS SOFTWARE IS PROVIDED "AS IS" AND WITH ALL FAULTS.
// NO WARRANTIES, WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT
// NOT LIMITED TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. TI SHALL NOT, UNDER ANY
// CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL, OR CONSEQUENTIAL
// DAMAGES, FOR ANY REASON WHATSOEVER.
//
// This is part of revision 10636 of the Stellaris Firmware Development Package.
//
//*****************************************************************************
#include <string.h>
#include "inc/hw_ethernet.h"
#include "inc/hw_flash.h"
#include "inc/hw_gpio.h"
#include "inc/hw_memmap.h"
#include "inc/hw_nvic.h"
#include "inc/hw_sysctl.h"
#include "inc/hw_types.h"
#include "bl_config.h"
#include "boot_loader/bl_crystal.h"
#include "boot_loader/bl_decrypt.h"
#include "boot_loader/bl_flash.h"
#include "boot_loader/bl_hooks.h"
//*****************************************************************************
//
//! \addtogroup bl_enet_api
//! @{
//
//*****************************************************************************
#if defined(ENET_ENABLE_UPDATE) || defined(DOXYGEN)
//*****************************************************************************
//
// Make sure that the crystal frequency is defined.
//
//*****************************************************************************
#if !defined(CRYSTAL_FREQ)
#error ERROR: CRYSTAL_FREQ must be defined for Ethernet update!
#endif
//*****************************************************************************
//
// Make sure that boot loader update is not enabled (it is not supported via
// BOOTP given that there is no way to distinguish between a normal firmware
// image and a boot loader update image).
//
//*****************************************************************************
#if defined(ENABLE_BL_UPDATE)
#error ERROR: Updating the boot loader is not supported over Ethernet!
#endif
//*****************************************************************************
//
// TFTP packets contain 512 bytes of data and a packet shorter than this
// indicates the end of the transfer.
//
//*****************************************************************************
#define TFTP_BLOCK_SIZE 512
//*****************************************************************************
//
// uIP uses memset, so a simple one is provided here. This is not as efficient
// as the one in the C library (from an execution time perspective), but it is
// much smaller.
//
//*****************************************************************************
void *
my_memset(void *pvDest, int iChar, size_t lLength)
{
char *pcBuf = (char *)pvDest;
//
// Fill the buffer with the given character.
//
while(lLength--)
{
*pcBuf++ = iChar;
}
//
// Return a pointer to the beginning of the buffer.
//
return(pvDest);
}
//*****************************************************************************
//
// uIP uses memcpy, so a simple one is provided here. This is not as efficient
// as the one in the C library (from an execution time perspective), but it is
// much smaller.
//
//*****************************************************************************
void *
my_memcpy(void *pvDest, const void *pvSrc, size_t lLength)
{
const char *pcSrc = (const char *)pvSrc;
char *pcDest = (char *)pvDest;
//
// Copy bytes from the source buffer to the destination buffer.
//
while(lLength--)
{
*pcDest++ = *pcSrc++;
}
//
// Return a pointer to the beginning of the destination buffer.
//
return(pvDest);
}
//*****************************************************************************
//
// Directly include the uIP code if using Ethernet for the update. This allows
// non-Ethernet boot loader builds to not have to supply the uip-conf.h file
// that would otherwise be required.
//
//*****************************************************************************
#define memcpy my_memcpy
#define memset my_memset
#include "third_party/uip-1.0/uip/pt.h"
#include "third_party/uip-1.0/uip/uip_arp.c"
#undef BUF
#include "uip.c" // JLK - use the local copy which has been edited
//*****************************************************************************
//
// A prototype for the function (in the startup code) for a predictable length
// delay.
//
//*****************************************************************************
extern void Delay(unsigned long ulCount);
//*****************************************************************************
//
// Defines for setting up the system clock.
//
//*****************************************************************************
#define SYSTICKHZ 100
#define SYSTICKMS (1000 / SYSTICKHZ)
//*****************************************************************************
//
// UIP Timers (in ms)
//
//*****************************************************************************
#define UIP_PERIODIC_TIMER_MS 50
#define UIP_ARP_TIMER_MS 10000
//*****************************************************************************
//
// This structure defines the fields in a BOOTP request/reply packet.
//
//*****************************************************************************
typedef struct
{
//
// The operation; 1 is a request, 2 is a reply.
//
unsigned char ucOp;
//
// The hardware type; 1 is Ethernet.
//
unsigned char ucHType;
//
// The hardware address length; for Ethernet this will be 6, the length of
// the MAC address.
//
unsigned char ucHLen;
//
// Hop count, used by gateways for cross-gateway booting.
//
unsigned char ucHops;
//
// The transaction ID.
//
unsigned long ulXID;
//
// The number of seconds elapsed since the client started trying to boot.
//
unsigned short usSecs;
//
// The BOOTP flags.
//
unsigned short usFlags;
//
// The client's IP address, if it knows it.
//
unsigned long ulCIAddr;
//
// The client's IP address, as assigned by the BOOTP server.
//
unsigned long ulYIAddr;
//
// The TFTP server's IP address.
//
unsigned long ulSIAddr;
//
// The gateway IP address, if booting cross-gateway.
//
unsigned long ulGIAddr;
//
// The hardware address; for Ethernet this is the MAC address.
//
unsigned char pucCHAddr[16];
//
// The name, or nickname, of the server that should handle this BOOTP
// request.
//
char pcSName[64];
//
// The name of the boot file to be loaded via TFTP.
//
char pcFile[128];
//
// Optional vendor-specific area; not used for BOOTP.
//
unsigned char pucVend[64];
}
tBOOTPPacket;
//*****************************************************************************
//
// The BOOTP commands.
//
//*****************************************************************************
#define BOOTP_REQUEST 1
#define BOOTP_REPLY 2
//*****************************************************************************
//
// The TFTP commands.
//
//*****************************************************************************
#define TFTP_RRQ 1
#define TFTP_WRQ 2
#define TFTP_DATA 3
#define TFTP_ACK 4
#define TFTP_ERROR 5
//*****************************************************************************
//
// The UDP ports used by the BOOTP protocol.
//
//*****************************************************************************
#define PORT_OFFSET 40000 // JLK
#define BOOTP_SERVER_PORT (67 + PORT_OFFSET)
#define BOOTP_CLIENT_PORT (68 + PORT_OFFSET)
//*****************************************************************************
//
// The UDP port for the TFTP server.
//
//*****************************************************************************
#define TFTP_PORT (69 + PORT_OFFSET)
//*****************************************************************************
//
// The MAC address of the Ethernet interface.
//
//*****************************************************************************
#ifdef ENET_MAC_ADDR0
static struct uip_eth_addr g_sMACAddr =
{
{
ENET_MAC_ADDR0,
ENET_MAC_ADDR1,
ENET_MAC_ADDR2,
ENET_MAC_ADDR3,
ENET_MAC_ADDR4,
ENET_MAC_ADDR5
}
};
#else
static struct uip_eth_addr g_sMACAddr;
#endif
//*****************************************************************************
//
// The number of SysTick interrupts since the start of the boot loader.
//
//*****************************************************************************
static unsigned long g_ulTicks;
//*****************************************************************************
//
// The seed for the random number generator.
//
//*****************************************************************************
static unsigned long g_ulRandomSeed;
//*****************************************************************************
//
// The number of milliseconds since the last call to uip_udp_periodic().
//
//*****************************************************************************
static volatile unsigned long g_ulPeriodicTimer;
//*****************************************************************************
//
// The number of milliseconds since the last call to uip_arp_timer().
//
//*****************************************************************************
static volatile unsigned long g_ulARPTimer;
//*****************************************************************************
//
// The transaction ID of the most recently sent out BOOTP request.
//
//*****************************************************************************
static unsigned long g_ulXID;
//*****************************************************************************
//
// The state for the proto-thread that handles the BOOTP process.
//
//*****************************************************************************
static struct pt g_sThread;
//*****************************************************************************
//
// The amount of time to wait for a BOOTP reply before sending out a new BOOTP
// request.
//
//*****************************************************************************
static unsigned long g_ulDelay;
//*****************************************************************************
//
// The target time (relative to g_ulTicks) when the next timeout occurs.
//
//*****************************************************************************
static unsigned long g_ulTarget;
//*****************************************************************************
//
// The IP address of the TFTP server.
//
//*****************************************************************************
static uip_ipaddr_t g_sServerAddr;
//*****************************************************************************
//
// The name of the file to be read from the TFTP server.
//
//*****************************************************************************
static char g_pcFilename[128];
//*****************************************************************************
//
// The end of flash. If there is not a reserved block at the end of flash,
// this is the real end of flash. If there is a reserved block, this is the
// start of the reserved block (i.e. the virtual end of flash).
//
//*****************************************************************************
static unsigned long g_ulFlashEnd;
//*****************************************************************************
//
// The current block being read from the TFTP server.
//
//*****************************************************************************
static unsigned long g_ulTFTPBlock;
//*****************************************************************************
//
// The UDP socket used to communicate with the BOOTP and TFTP servers (in
// sequence).
//
//*****************************************************************************
struct uip_udp_conn *g_pConn;
//*****************************************************************************
//
//! Handles the SysTick interrupt.
//!
//! This function is called when the SysTick interrupt occurs. It simply
//! keeps a running count of interrupts, used as a time basis for the BOOTP and
//! TFTP protocols.
//!
//! \return None.
//
//*****************************************************************************
void
SysTickIntHandler(void)
{
//
// Increment the tick count.
//
g_ulTicks++;
g_ulPeriodicTimer += SYSTICKMS;
g_ulARPTimer += SYSTICKMS;
}
//*****************************************************************************
//
//! Computes a new random number.
//!
//! This function computes a new pseudo-random number, using a linear
//! congruence random number generator. Note that if the entire 32-bits of the
//! produced random number are not being used, the upper N bits should be used
//! instead of the lower N bits as they are much more random (for example, use
//! ``RandomNumber() >> 28'' instead of ``RandomNumber() & 15'').
//!
//! \return Returns a 32-bit pseudo-random number.
//
//*****************************************************************************
static unsigned long
RandomNumber(void)
{
//
// Generate a new pseudo-random number with a linear congruence random
// number generator. This new random number becomes the seed for the next
// random number.
//
g_ulRandomSeed = (g_ulRandomSeed * 1664525) + 1013904223;
//
// Return the new random number.
//
return(g_ulRandomSeed);
}
//*****************************************************************************
//
//! Reads a packet from the Ethernet controller.
//!
//! This function reads a packet from the Ethernet controller into the uIP
//! packet buffer. It assumes that a packet is available to be read.
//!
//! \return None.
//
//*****************************************************************************
static void
EnetReadPacket(void)
{
unsigned long ulTemp;
long lIdx, lCount;
//
// Read the first word from the FIFO.
//
ulTemp = HWREG(ETH_BASE + MAC_O_DATA);
//
// Extract the size of the remainder of the packet.
//
lCount = (ulTemp & 0xffff) - 4;
//
// See if the packet will fit into the data buffer.
//
if(lCount > (sizeof(uip_buf) + 2))
{
//
// The packet will not fit into the data buffer, so read the rest of
// the data and throw it away.
//
while(lCount > 0)
{
HWREG(ETH_BASE + MAC_O_DATA);
lCount -= 4;
}
//
// There is no packet for uIP to process.
//
uip_len = 0;
//
// Return without any further processing.
//
return;
}
//
// Copy the first two bytes into the packet buffer.
//
*(unsigned short *)uip_buf = ulTemp >> 16;
//
// Save the packet length for uIP's use.
//
uip_len = lCount - 2;
//
// Read the remainder of the packet data into uIP's buffer.
//
for(lIdx = 2; lIdx < uip_len; lIdx += 4)
{
*(unsigned long *)(uip_buf + lIdx) = HWREG(ETH_BASE + MAC_O_DATA);
}
//
// Read the final word from the FIFO, which contains part or all of the
// frame check sequence (which is ignored).
//
HWREG(ETH_BASE + MAC_O_DATA);
}
//*****************************************************************************
//
//! Writes a packet to the Ethernet controller.
//!
//! This function writes a packet from the uIP packet buffer into the Ethernet
//! controller and requests that the packet be transmitted. It will busy wait
//! until there is space in the transmit FIFO.
//!
//! \return None.
//
//*****************************************************************************
static void
EnetWritePacket(void)
{
long lIdx, lCount;
//
// Wait until there is space in the transmit FIFO.
//
while(HWREG(ETH_BASE + MAC_O_TR) & MAC_TR_NEWTX)
{
}
//
// Get the length of the packet.
//
lCount = uip_len;
//
// Write the first two bytes of the packet to the FIFO, along with the
// packet size.
//
HWREG(ETH_BASE + MAC_O_DATA) = ((*(unsigned short *)uip_buf << 16) |
(lCount - 14));
//
// Write the remaining bytes of the packet to the FIFO.
//
for(lIdx = 2; lIdx < lCount; lIdx += 4)
{
HWREG(ETH_BASE + MAC_O_DATA) = *(unsigned long *)(uip_buf + lIdx);
}
//
// Send the packet.
//
HWREG(ETH_BASE + MAC_O_TR) = MAC_TR_NEWTX;
}
//*****************************************************************************
//
//! Constructs and sends a BOOTP request packet.
//!
//! This function constructs a BOOTP request packet and sends it as a broadcast
//! message to the network.
//!
//! \return None.
//
//*****************************************************************************
static void
SendBOOTPRequest(void)
{
unsigned char *pucPacket = (unsigned char *)uip_appdata;
tBOOTPPacket *pBOOTP = (tBOOTPPacket *)uip_appdata;
unsigned long ulIdx;
//
// Zero fill the BOOTP request packet.
//
for(ulIdx = 0; ulIdx < sizeof(tBOOTPPacket); ulIdx++)
{
pucPacket[ulIdx] = 0;
}
//
// Construct a BOOTP request.
//
pBOOTP->ucOp = BOOTP_REQUEST;
//
// Set the hardware type to Ethernet.
//
pBOOTP->ucHType = 0x01;
//
// Set the hardware address length to 6.
//
pBOOTP->ucHLen = 0x06;
//
// Choose a random number for the transaction ID.
//
pBOOTP->ulXID = g_ulXID = RandomNumber();
//
// Set the number of seconds since we started.
//
pBOOTP->usSecs = HTONS(g_ulTicks / SYSTICKHZ);
//
// Fill in the Ethernet MAC address.
//
for(ulIdx = 0; ulIdx < 6; ulIdx++)
{
pBOOTP->pucCHAddr[ulIdx] = g_sMACAddr.addr[ulIdx];
}
//
// Set the server name if defined.
//
#ifdef ENET_BOOTP_SERVER
for(ulIdx = 0; (pBOOTP->pcSName[ulIdx] = ENET_BOOTP_SERVER[ulIdx]) != 0;
ulIdx++)
{
}
#endif
//
// Send the BOOTP request packet.
//
uip_udp_send(sizeof(tBOOTPPacket));
}
//*****************************************************************************
//
//! Parses a packet checking for a BOOTP reply message.
//!
//! This function parses a packet to determine if it is a BOOTP reply to our
//! currently outstanding BOOTP request. If a valid reply is found, the
//! appropriate information from the packet is extracted and saved.
//!
//! \return Returns 1 if a valid BOOTP reply message was found and 0 otherwise.
//
//*****************************************************************************
static unsigned long
ParseBOOTPReply(void)
{
tBOOTPPacket *pBOOTP = (tBOOTPPacket *)uip_appdata;
unsigned long ulIdx;
//
// See if this is a reply for our current BOOTP request.
//
if((pBOOTP->ucOp != BOOTP_REPLY) ||
(pBOOTP->ulXID != g_ulXID) ||
(*(unsigned long *)pBOOTP->pucCHAddr !=
*(unsigned long *)g_sMACAddr.addr) ||
(*(unsigned short *)(pBOOTP->pucCHAddr + 4) !=
*(unsigned short *)(g_sMACAddr.addr + 4)))
{
return(0);
}
//
// Extract our IP address from the response.
//
*((unsigned long *)(void *)(&uip_hostaddr)) = pBOOTP->ulYIAddr;
//
// Extract the server address from the response.
//
*((unsigned long *)(void *)(&g_sServerAddr)) = pBOOTP->ulSIAddr;
//
// Save the boot file name.
//
for(ulIdx = 0;
((g_pcFilename[ulIdx] = pBOOTP->pcFile[ulIdx]) != 0) &&
(ulIdx < (sizeof(g_pcFilename) - 1));
ulIdx++)
{
}
g_pcFilename[ulIdx] = 0;
//
// A valid BOOTP reply was found and decoded.
//
return(1);
}
//*****************************************************************************
//
//! Constructs and sends a TFTP error packet.
//!
//! This function constructs a TFTP read request packet (RRQ) and sends it to
//! the server.
//!
//! \return None.
//
//*****************************************************************************
static void
SendTFTPError(unsigned short usError, char *pcString)
{
unsigned char *pucPacket = (unsigned char *)uip_appdata;
long lLen;
pucPacket[0] = (TFTP_ERROR >> 8) & 0xff;
pucPacket[1] = TFTP_ERROR & 0xff;
pucPacket[2] = (usError >> 8) & 0xFF;
pucPacket[3] = usError & 0xFF;
//
// Get ready to copy the error string.
//
lLen = 4;
pucPacket += 4;
//
// Copy as much of the string as we can fit.
//
while((lLen < (UIP_APPDATA_SIZE - 1)) && *pcString)
{
*pucPacket++ = *pcString++;
lLen++;
}
//
// Write the terminating 0.
//
*pucPacket = (unsigned char)0;
//
// Send the error packet.
//
uip_udp_send(lLen + 1);
}
//*****************************************************************************
//
//! Constructs and sends a TFTP read packet.
//!
//! This function constructs a TFTP read request packet (RRQ) and sends it to
//! the server.
//!
//! \return None.
//
//*****************************************************************************
static void
SendTFTPGet(void)
{
unsigned char *pucPacket = (unsigned char *)uip_appdata;
unsigned long ulIdx;
char *pcFilename;
//
// The TFTP RRQ packet should be sent to the TFTP server port.
//
g_pConn->rport = HTONS(TFTP_PORT);
//
// Set the TFTP packet opcode to RRQ.
//
pucPacket[0] = (TFTP_RRQ >> 8) & 0xff;
pucPacket[1] = TFTP_RRQ & 0xff;
//
// Copy the file name into the RRQ packet.
//
for(ulIdx = 2, pcFilename = g_pcFilename;
(pucPacket[ulIdx++] = *pcFilename++) != 0; )
{
}
//
// Set the transfer mode to binary.
//
for(pcFilename = "octet"; (pucPacket[ulIdx++] = *pcFilename++) != 0; )
{
}
//
// Send the TFTP read packet.
//
uip_udp_send(ulIdx);
}
//*****************************************************************************
//
//! Parses a packet checking for a TFTP data packet.
//!
//! This function parses a packet to determine if it is a TFTP data packet for
//! out current TFTP transfer. If a valid packet is found, the contents of the
//! packet are programmed into flash.
//!
//! \return Returns 1 if this packet was the last packet of the TFTP data
//! transfer and 0 otherwise.
//
//*****************************************************************************
static unsigned long
ParseTFTPData(void)
{
unsigned char *pucPacket = (unsigned char *)uip_appdata;
unsigned long ulFlashAddr;
unsigned long ulIdx;
//
// See if this is a TFTP data packet.
//
if((pucPacket[0] != ((TFTP_DATA >> 8) && 0xff)) ||
(pucPacket[1] != (TFTP_DATA & 0xff)))
{
return(0);
}
//
// If the remote port on our connection is still the TFTP server port (i.e.
// this is the first data packet), then copy the transaction ID for the
// TFTP data connection into our connection. This will ensure that our
// response will be sent to the correct port.
//
if(g_pConn->rport == HTONS(TFTP_PORT))
{
g_pConn->rport =
((struct uip_udpip_hdr *)&uip_buf[UIP_LLH_LEN])->srcport;
}
//
// See if this is the correct data packet.
//
if((pucPacket[2] != ((g_ulTFTPBlock >> 8) & 0xff)) ||
(pucPacket[3] != (g_ulTFTPBlock & 0xff)))
{
//
// Since the wrong data packet was sent, resend the ACK for it since
// we've already processed it.
//
pucPacket[0] = (TFTP_ACK >> 8) & 0xff;
pucPacket[1] = TFTP_ACK & 0xff;
uip_udp_send(4);
//
// Ignore this packet.
//
return(0);
}
//
// What address are we about to program to?
//
ulFlashAddr = ((g_ulTFTPBlock - 1) * TFTP_BLOCK_SIZE) + APP_START_ADDRESS;
//
// Do not program this data into flash if it is beyond the end of flash.
//
if(ulFlashAddr < g_ulFlashEnd)
{
//
// If this is the first block and we have been provided with a start
// hook function, call it here to indicate that we are about to begin
// flashing a new image.
//
#ifdef BL_START_FN_HOOK
if(g_ulTFTPBlock == 1)
{
BL_START_FN_HOOK();
}
#endif
//
// Clear any flash error indicator.
//
BL_FLASH_CL_ERR_FN_HOOK();
//
// If this is the first data packet and code protection is enabled,
// then erase the entire flash.
//
#ifdef FLASH_CODE_PROTECTION
if(g_ulTFTPBlock == 1)
{
//
// Loop through the pages in the flash, excluding the pages that
// contain the boot loader and the optional reserved space.
//
for(ulIdx = APP_START_ADDRESS; ulIdx < g_ulFlashEnd;
ulIdx += FLASH_PAGE_SIZE)
{
//
// Erase this block of the flash.
//
BL_FLASH_ERASE_FN_HOOK((ulIdx);
}
}
#else
//
// Flash code protection is not enabled, so see if the data in this
// packet will be programmed to the beginning of a flash block. We
// assume that the flash block size is always a multiple of 1KB so,
// since each TFTP packet is 512 bytes and that the start must always
// be on a flash page boundary, we can be sure that we will hit the
// start of each page as we receive packets.
//
if(!(ulFlashAddr & (FLASH_PAGE_SIZE - 1)))
{
//
// Erase this block of the flash.
//
BL_FLASH_ERASE_FN_HOOK(ulFlashAddr);
}
#endif
//
// Decrypt the data if required.
//
#ifdef BL_DECRYPT_FN_HOOK
BL_DECRYPT_FN_HOOK(pucPacket + 4, uip_len - 4);
#endif
//
// Program this block of data into flash.
//
BL_FLASH_PROGRAM_FN_HOOK(ulFlashAddr, (pucPacket + 4), (uip_len - 4));
//
// If a progress reporting hook function has been provided, call it
// here. The TFTP protocol doesn't let us know how large the image is
// before it starts the transfer so we pass 0 as the ulTotal parameter
// to indicate this.
//
#ifdef BL_PROGRESS_FN_HOOK
BL_PROGRESS_FN_HOOK(((ulFlashAddr - APP_START_ADDRESS) + (uip_len - 4)),
0);
#endif
}
//
// Increment to the next block.
//
g_ulTFTPBlock++;
//
// Save the packet length.
//
ulIdx = uip_len;
//
// Did we see any error?
//
if(BL_FLASH_ERROR_FN_HOOK())
{
//
// Yes - send back an error packet.
//
SendTFTPError(2, "Error programming flash.");
}
else
{
//
// No errors reported so construct an ACK packet. The block number
// field is already correct, so it does not need to be set.
//
pucPacket[0] = (TFTP_ACK >> 8) & 0xff;
pucPacket[1] = TFTP_ACK & 0xff;
//
// Send the ACK packet to the TFTP server.
//
uip_udp_send(4);
}
//
// If the packet was shorter than TFTP_BLOCK_SIZE bytes then this was the
// last packet in the file.
//
if(ulIdx != (TFTP_BLOCK_SIZE + 4))
{
//
// If an end signal hook function has been provided, call it here.