Callisto4Bootloader: lib/c4_tftp.c File Reference

Callisto4Bootloader

Macros | Enumerations | Functions | Variables
c4_tftp.c File Reference

Implementation of TFTP protocol to send and receive application binary, application configuration, operation logs, and error logs. More...

`#include "c4_tftp.h"`
`#include "lwip/tcpip.h"`
`#include "lwip/netif.h"`
`#include "lwip/sockets.h"`
`#include "enet_ethernetif.h"`

Go to the source code of this file.

## Macros

#define | max(a, b)
| Maximum. More...
#define | min(a, b)
| Minimum. More...
#define | EXAMPLE_CLOCK_NAME kCLOCK_CoreSysClk

## Enumerations

enum | tftp_opcode_t { E_TFTP_OP_RRQ = 1, E_TFTP_OP_WRQ = 2, E_TFTP_OP_DATA = 3, E_TFTP_OP_ACK = 4, E_TFTP_OP_ERROR = 5, E_TFTP_OP_OACK = 6, E_TFTP_OP_NUM }
| TFTP opcodes. More...
enum | tftp_mode_t { E_TFTP_MODE_TEXT, E_TFTP_MODE_BINARY, E_TFTP_MODE_MAIL, E_TFTP_MODE_NUM }
| enumeration of supported TFTP modes More...
enum | tftp_err_t { E_TFTP_ERR_UNDEF = 0, E_TFTP_ERR_NOT_FOUND = 1, E_TFTP_ERR_ACCESS = 2, E_TFTP_ERR_ALLOC = 3, E_TFTP_ERR_ILLEGAL = 4, E_TFTP_ERR_BAD_TRANSFER = 5, E_TFTP_ERR_EXISTS = 6, E_TFTP_ERR_USER = 7, E_TFTP_ERR_OPT = 8, E_TFTP_ERR_NUM }
| TFTP error codes. More...
enum | tftp_opt_t { E_TFTP_OPT_BLKSIZE, E_TFTP_OPT_TIMEOUT, E_TFTP_OPT_TSIZE, E_TFTP_OPT_NUM }
| TFTP Options. More...

## Functions

static void | ipaddress_show (void)
| Display the current IP address, subnet mask, and gateway address. More...
static int | TFTP_makePacketAck (uint16_t block, uint8_t *packet, size_t max_pkt)
| Construct TFTP acknowledge packet. More...
static int | TFTP_makePacketData (uint16_t block, const uint8_t *data, size_t len, uint8_t *packet, size_t max_pkt)
| Construct TFTP data packet. More...
static int | TFTP_makePacketErr (tftp_err_t code, const char *msg, uint8_t *packet, size_t max_pkt)
| Construct TFTP error packet. More...
static int | TFTP_makePacketRQ (const char *name, tftp_opcode_t op, tftp_mode_t mode, uint8_t *packet, size_t max_pkt)
| Construct TFTP read or write request packet. More...
static int | TFTP_parsePacketAck (const uint8_t *packet, size_t len, uint16_t *block)
| Parse TFTP Acknowledge packet. More...
static int | TFTP_parsePacketData (const uint8_t *packet, size_t len, uint16_t *block, uint8_t **pData)
| Parse TFTP data packet. More...
static int | TFTP_parsePacketError (const uint8_t *packet, size_t len, tftp_err_t *errcode, char **pMsg)
| Parse TFTP error request packet. More...
static int | TFTP_parsePacketOptAck (const uint8_t *packet, size_t len, int *blocksize, int *timeout, int *totalsize)
| Parse TFTP Option acknowledgement packet. More...
exit_code_t | c4_tftpDownloadText (const uint8_t server[4], const char *name, char *file, uint32_t max_file)
| Use TFTP to download the configuration file. More...
exit_code_t | c4_stack_init (const uint8_t au8MACAddress[6], const uint8_t au8StaticIPAddress[4], const uint8_t au8StaticNetMask[4], const uint8_t au8StaticGateway[4])
| Initialise the IP stack. More...

## Variables

static const uint32_t | TFTP_PORT = 69
| Well known TFTP port. More...
static const uint16_t | TFTP_DATA_MAX = 1020
| Maximum TFTP packet size. More...
static const uint16_t | TFTP_PACKET_MAX = (TFTP_DATA_MAX + 4)
| Maximum TFTP packet size. More...
const char * | TFTP_MODE [E_TFTP_MODE_NUM] = {"netascii", "octet", "mail"}
| TFTP mode strings. More...
const char * | TFTP_OPT [E_TFTP_OPT_NUM] = {"blksize", "timeout", "tsize"}
| TFTP option strings. More...
static struct netif | fsl_netif1
static ip4_addr_t | fsl_netif1_ipaddr
static ip4_addr_t | fsl_netif1_netmask
static ip4_addr_t | fsl_netif1_gw

## Detailed Description

Implementation of TFTP protocol to send and receive application binary, application configuration, operation logs, and error logs.

Copyright
  2020 Remsdaq Ltd.

Date
  10 Sep 2020

Author
  Dan Smithers dwhs@.nosp@m.rems.nosp@m.daq.c.nosp@m.om

TFTP Formats using RFC 1350 with options as specified in RFC 2847-9

Type | Op # | Format without header
--- | --- | ---
RRQ | 2 bytes | string | 1 byte | string | 1 byte | string | 1 byte | ... | string | 1 byte | string | 1 byte
01 | Filename | 0 | Opt 1 | 0 | Val 1 | 0 | Opt n | 0 | Val n | 0
WRQ | 2 bytes | string | 1 byte | string | 1 byte | string | 1 byte | ... | string | 1 byte | string | 1 byte
02 | Filename | 0 | Opt 1 | 0 | Val 1 | 0 | Opt n | 0 | Val n | 0
DATA | 2 bytes | 2 bytes | n bytes |
03 | Block # | Data |
ACK | 2 bytes | 2 bytes |
04 | Block # |
ERROR | 2 bytes | 2 bytes | string | 1 byte |
05 | Error Code | ErrMsg | 0 |
OACK | 2 bytes | string | 1 byte | string | 1 byte | ... | string | 1 byte | string | 1 byte |
06 | Opt 1 | 0 | Val 1 | 0 | Opt n | 0 | Val n | 0 |

Definition in file c4_tftp.c.

## Macro Definition Documentation

## ◆ EXAMPLE_CLOCK_NAME

#define EXAMPLE_CLOCK_NAME kCLOCK_CoreSysClk

System clock name.

Definition at line 75 of file c4_tftp.c.

## ◆ max

#define max | ( |  | a,
|  |  | b
| ) |  |

Value:
({ \
__typeof__ (a) _a = (a); \
__typeof__ (b) _b = (b); \
_a > _b ? _a : _b; \
})

Maximum.

Parameters

  [in] | a | first value
  [in] | b | second value

Returns
  a if a > b otherwise b

Definition at line 51 of file c4_tftp.c.

## ◆ min

#define min | ( |  | a,
|  |  | b
| ) |  |

Value:
({ \
__typeof__ (a) _a = (a); \
__typeof__ (b) _b = (b); \
_a < _b ? _a : _b; \
})

Minimum.

Parameters

  [in] | a | first value
  [in] | b | second value

Returns
  a if a < b otherwise b

Definition at line 66 of file c4_tftp.c.

## Enumeration Type Documentation

## ◆ tftp_err_t

enum tftp_err_t

TFTP error codes.

Enumerator
E_TFTP_ERR_UNDEF | Not defined, see error message (if any).
E_TFTP_ERR_NOT_FOUND | File not found.
E_TFTP_ERR_ACCESS | Access violation.
E_TFTP_ERR_ALLOC | Disk full or allocation exceeded.
E_TFTP_ERR_ILLEGAL | Illegal TFTP operation.
E_TFTP_ERR_BAD_TRANSFER | Unknown transfer ID.
E_TFTP_ERR_EXISTS | File already exists.
E_TFTP_ERR_USER | No such user.
E_TFTP_ERR_OPT | Option error.
E_TFTP_ERR_NUM | Number of supported TFTP error codes.

Definition at line 107 of file c4_tftp.c.

## ◆ tftp_mode_t

enum tftp_mode_t

enumeration of supported TFTP modes

Enumerator
E_TFTP_MODE_TEXT | Text mode.
E_TFTP_MODE_BINARY | Binary mode.
E_TFTP_MODE_MAIL | Mail mode.
E_TFTP_MODE_NUM | Number of supported TFTP modes.

Definition at line 98 of file c4_tftp.c.

## ◆ tftp_opcode_t

enum tftp_opcode_t

TFTP opcodes.

Enumerator
E_TFTP_OP_RRQ | TFTP Read request.
E_TFTP_OP_WRQ | TFTP Write request.
E_TFTP_OP_DATA | TFTP Data packet.
E_TFTP_OP_ACK | TFTP Acknowledge packet.
E_TFTP_OP_ERROR | TFTP Error packet.
E_TFTP_OP_OACK | TFTP Optiona Acknowledge packet.
E_TFTP_OP_NUM | Number of supported TFTP op codes.

Definition at line 86 of file c4_tftp.c.

## ◆ tftp_opt_t

enum tftp_opt_t

TFTP Options.

Enumerator
E_TFTP_OPT_BLKSIZE | Negotiate blocksize.
E_TFTP_OPT_TIMEOUT | Negotiate timeout.
E_TFTP_OPT_TSIZE | Negotiate transfer size total.
E_TFTP_OPT_NUM | Total number of TFTP options.

Definition at line 122 of file c4_tftp.c.

## Function Documentation

## ◆ c4_stack_init()

exit_code_t c4_stack_init | ( | const uint8_t | au8MACAddress[6],
|  | const uint8_t | au8StaticIPAddress[4],
|  | const uint8_t | au8StaticNetMask[4],
|  | const uint8_t | au8StaticGateway[4]
| ) |  |

Initialise the IP stack.

Starts LWIP, TCPIP, IGMP, etc

Definition at line 377 of file c4_tftp.c.

## ◆ c4_tftpDownloadText()

exit_code_t c4_tftpDownloadText | ( | const uint8_t | server[4],
|  | const char * | name,
|  | char * | file,
|  | uint32_t | max_file
| ) |  |

Use TFTP to download the configuration file.

The download function determines whether the file is a location in RAM or on QSPI flash.

If the location is in RAM, then the TFTP transfer will be made directly into the memory starting at `file` and `max_file` is the length already allocated at that address. Data will overwrite anything that is already stored in this location.

If the location is in QSPI flash, then `file` specifies the location to receive the data and `max_file` is the extent available. In this case, the region specified by these two parameters will be erased and the data received written block-by-block.

Definition at line 266 of file c4_tftp.c.

## ◆ ipaddress_show()

static void ipaddress_show | ( | void |  | ) |

static

Display the current IP address, subnet mask, and gateway address.

## ◆ TFTP_makePacketAck()

static int TFTP_makePacketAck | ( | uint16_t | block,
|  | uint8_t * | packet,
|  | size_t | max_pkt
| ) |  |

static

Construct TFTP acknowledge packet.

Parameters

  [in] | block | Block number to acknowledge
  [out] | packet | Array to accept formatted packet.
  [in] | max_pkt | Length of packet

Returns
  length of packet constructed.

Definition at line 483 of file c4_tftp.c.

## ◆ TFTP_makePacketData()

static int TFTP_makePacketData | ( | uint16_t | block,
|  | const uint8_t * | data,
|  | size_t | len,
|  | uint8_t * | packet,
|  | size_t | max_pkt
| ) |  |

static

Construct TFTP data packet.

Parameters

  [in] | block | Block number. This is the block number of this data packet
  [in] | data | Pointer to data
  [in] | len | Number of bytes to write
  [out] | packet | Array to accept formatted packet.
  [in] | max_pkt | Length of packet

Returns
  length of packet constructed.

Definition at line 494 of file c4_tftp.c.

## ◆ TFTP_makePacketErr()

static int TFTP_makePacketErr | ( | tftp_err_t | code,
|  | const char * | msg,
|  | uint8_t * | packet,
|  | size_t | max_pkt
| ) |  |

static

Construct TFTP error packet.

Parameters

  [in] | code | Error code.
  [in] | msg | Error string
  [out] | packet | Array to accept formatted packet.
  [in] | max_pkt | Length of packet

Returns
  length of packet constructed.

Definition at line 502 of file c4_tftp.c.

## ◆ TFTP_makePacketRQ()

static int TFTP_makePacketRQ | ( | const char * | name,
|  | tftp_opcode_t | op,
|  | tftp_mode_t | mode,
|  | uint8_t * | packet,
|  | size_t | max_pkt
| ) |  |

static

Construct TFTP read or write request packet.

Parameters

  [in] | name | File name to read or write.
  [in] | op | Read or write opcode
  [in] | mode | Transfer mode
  [out] | packet | Array to accept formatted packet.
  [in] | max_pkt | Length of packet

Returns
  length of packet constructed.

Definition at line 522 of file c4_tftp.c.

## ◆ TFTP_parsePacketAck()

static int TFTP_parsePacketAck | ( | const uint8_t * | packet,
|  | size_t | len,
|  | uint16_t * | block
| ) |  |

static

Parse TFTP Acknowledge packet.

Parameters

  [in] | packet | Pointer to data
  [in] | len | Number of bytes in packet
  [out] | block | Block number acknowwledged.

Returns
  length of packet constructed.

Definition at line 551 of file c4_tftp.c.

## ◆ TFTP_parsePacketData()

static int TFTP_parsePacketData | ( | const uint8_t * | packet,
|  | size_t | len,
|  | uint16_t * | block,
|  | uint8_t ** | pData
| ) |  |

static

Parse TFTP data packet.

The TFTP data packet contains the block number and up to the negotiated packet size bytes of data. Packets may be repeated if an acknowledgement has not been received.

`block` contains the last received block and is used to determine whether this packet contains new data. If this is the next block, so it is updated and `pData` is updated to the start of the new data.

If this packet contains duplicate data, `pData` is set to NULL and the caller must

The return value is always the length of data received so the retrieving loop can determine when the transmission is complete.

Parameters

  [in] | packet | Pointer to packet to parse
  [in] | len | Number of bytes in packet
  [in,out] | block | Block number. This contains the last block successfully received and is both input and output.
  [out] | pData | Pointer to the start of data, or NULL.

Returns
  length of data received.

Definition at line 563 of file c4_tftp.c.

## ◆ TFTP_parsePacketError()

static int TFTP_parsePacketError | ( | const uint8_t * | packet,
|  | size_t | len,
|  | tftp_err_t * | errcode,
|  | char ** | pMsg
| ) |  |

static

Parse TFTP error request packet.

Parameters

  [in] | packet | Array to accept formatted packet.
  [in] | len | Number of bytes in packet
  [out] | errcode | Error code.
  [out] | pMsg | Pointer to error string.

Returns
  length of error string received.

Definition at line 584 of file c4_tftp.c.

## ◆ TFTP_parsePacketOptAck()

static int TFTP_parsePacketOptAck | ( | const uint8_t * | packet,
|  | size_t | len,
|  | int * | blocksize,
|  | int * | timeout,
|  | int * | totalsize
| ) |  |

static

Parse TFTP Option acknowledgement packet.

Parameters

  [in] | packet | Array to accept formatted packet.
  [in] | len | Number of bytes in packet
  [out] | blocksize | Blocksize received.
  [out] | timeout | Timout received
  [out] | totalsize | Total data size received.

Returns
  length of packet constructed.

Definition at line 598 of file c4_tftp.c.

## Variable Documentation

## ◆ fsl_netif1

struct netif fsl_netif1

static

Definition at line 245 of file c4_tftp.c.

## ◆ fsl_netif1_gw

ip4_addr_t fsl_netif1_gw

static

Definition at line 248 of file c4_tftp.c.

## ◆ fsl_netif1_ipaddr

ip4_addr_t fsl_netif1_ipaddr

static

Definition at line 246 of file c4_tftp.c.

## ◆ fsl_netif1_netmask

ip4_addr_t fsl_netif1_netmask

static

Definition at line 247 of file c4_tftp.c.

## ◆ TFTP_DATA_MAX

const uint16_t TFTP_DATA_MAX = 1020

static

Maximum TFTP packet size.

Definition at line 77 of file c4_tftp.c.

## ◆ TFTP_MODE

const char* TFTP_MODE[E_TFTP_MODE_NUM] = {"netascii", "octet", "mail"}

TFTP mode strings.

Definition at line 131 of file c4_tftp.c.

## ◆ TFTP_OPT

const char* TFTP_OPT[E_TFTP_OPT_NUM] = {"blksize", "timeout", "tsize"}

TFTP option strings.

Definition at line 133 of file c4_tftp.c.

## ◆ TFTP_PACKET_MAX

const uint16_t TFTP_PACKET_MAX = (TFTP_DATA_MAX + 4)

static

Maximum TFTP packet size.

Definition at line 78 of file c4_tftp.c.

## ◆ TFTP_PORT

const uint32_t TFTP_PORT = 69

static

Well known TFTP port.

Definition at line 76 of file c4_tftp.c.