tftp.fs 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. ( TFTP JCB 09:16 11/11/10)
  2. variable blocknum
  3. : tftp-ack ( -- )
  4. d# 2 ETH.IP.SRCIP mac-inoffset mac@n arp-lookup if
  5. ETH.IP.UDP.SOURCEPORT packet@
  6. d# 1077
  7. d# 2 ETH.IP.SRCIP mac-inoffset mac@n
  8. net-my-ip
  9. 2over arp-lookup
  10. ( dst-port src-port dst-ip src-ip *ethaddr )
  11. udp-header
  12. d# 4 mac-pkt-,
  13. blocknum @ mac-pkt-,
  14. udp-wrapup mac-send
  15. then
  16. ;
  17. : tftp-handler ( -- )
  18. IP_PROTO_UDP ip-isproto if
  19. OFFSET_UDP_DESTPORT packet@ d# 69 = if
  20. udp-checksum? if
  21. ETH.IP.UDP.TFTP.OPCODE packet@
  22. s" tftp opcode=" type dup hex4 cr
  23. dup d# 2 = if
  24. s" WRQ filename: " type
  25. ETH.IP.UDP.TFTP.RWRQ.FILENAME mac-inoffset d# 32 mac-dump
  26. d# 0 blocknum !
  27. tftp-ack
  28. then
  29. drop
  30. then
  31. then
  32. OFFSET_UDP_DESTPORT packet@ d# 1077 = if
  33. udp-checksum? if
  34. ETH.IP.UDP.TFTP.OPCODE packet@
  35. s" tftp opcode=" type dup hex4 cr
  36. dup d# 3 = if
  37. s" tftp recv=" type ETH.IP.UDP.TFTP.DATA.BLOCK packet@ hex4 s" expected=" type blocknum @ 1+ hex4 cr
  38. blocknum @ 1+
  39. ETH.IP.UDP.TFTP.DATA.BLOCK packet@ = if
  40. \ data at ETH.IP.UDP.TFTP.DATA.DATA
  41. ETH.IP.UDP.TFTP.DATA.DATA mac-inoffset
  42. blocknum @ d# 9 lshift h# 2000 +
  43. d# 256 0do
  44. over mac@ h# 5555 xor over h# 3ffe min !
  45. 2+ swap 2+ swap
  46. loop
  47. 2drop
  48. d# 1 blocknum +!
  49. tftp-ack
  50. ETH.IP.UDP.LENGTH packet@ d# 12 - 0= if
  51. h# 2000 h# 100 dump
  52. bootloader
  53. then
  54. else
  55. s" unexpected blocknum" type cr
  56. tftp-ack
  57. then
  58. then
  59. drop
  60. then
  61. then
  62. then
  63. ;