1 #include <syslinux/pxe_api.h>
8 /* Common receive buffer */
9 static __lowmem
char packet_buf
[PKTBUF_SIZE
] __aligned(16);
11 extern uint16_t get_port(void);
12 extern void free_port(uint16_t);
14 const struct url_scheme url_schemes
[] = {
15 { "tftp", tftp_open
, 0 },
22 * @param:socket, the socket to open
24 * @out: error code, 0 on success, -1 on failure
26 int core_udp_open(struct pxe_pvt_inode
*socket __unused
)
28 struct net_private_tftp
*priv
= &socket
->net
.tftp
;
30 /* Allocate local UDP port number */
31 priv
->localport
= get_port();
39 * @param:socket, the socket to open
41 void core_udp_close(struct pxe_pvt_inode
*socket
)
43 struct net_private_tftp
*priv
= &socket
->net
.tftp
;
46 free_port(priv
->localport
);
50 * Establish a connection on an open socket
52 * @param:socket, the open socket
53 * @param:ip, the ip address
54 * @param:port, the port number, host-byte order
56 void core_udp_connect(struct pxe_pvt_inode
*socket
, uint32_t ip
,
59 struct net_private_tftp
*priv
= &socket
->net
.tftp
;
61 socket
->tftp_remoteport
= htons(port
);
67 * Tear down a connection on an open socket
69 * @param:socket, the open socket
71 void core_udp_disconnect(struct pxe_pvt_inode
*socket __unused
)
76 * Read data from the network stack
78 * @param:socket, the open socket
79 * @param:buf, location of buffer to store data
80 * @param:buf_len, size of buffer
82 * @out: src_ip, ip address of the data source
83 * @out: src_port, port number of the data source, host-byte order
85 int core_udp_recv(struct pxe_pvt_inode
*socket
, void *buf
, uint16_t *buf_len
,
86 uint32_t *src_ip
, uint16_t *src_port
)
88 static __lowmem
struct s_PXENV_UDP_READ udp_read
;
89 struct net_private_tftp
*priv
= &socket
->net
.tftp
;
94 udp_read
.buffer
= FAR_PTR(packet_buf
);
95 udp_read
.buffer_size
= PKTBUF_SIZE
;
96 udp_read
.dest_ip
= IPInfo
.myip
;
97 udp_read
.d_port
= priv
->localport
;
99 err
= pxe_call(PXENV_UDP_READ
, &udp_read
);
104 return udp_read
.status
;
106 bytes
= min(udp_read
.buffer_size
, *buf_len
);
107 memcpy(buf
, packet_buf
, bytes
);
109 *src_ip
= udp_read
.src_ip
;
110 *src_port
= ntohs(udp_read
.s_port
);
119 * @param:socket, the open socket
120 * @param:data, data buffer to send
121 * @param:len, size of data bufer
123 void core_udp_send(struct pxe_pvt_inode
*socket
, const void *data
, size_t len
)
125 static __lowmem
struct s_PXENV_UDP_WRITE udp_write
;
126 struct net_private_tftp
*priv
= &socket
->net
.tftp
;
134 memcpy(lbuf
, data
, len
);
136 tid
= priv
->localport
; /* TID(local port No) */
137 udp_write
.buffer
= FAR_PTR(lbuf
);
138 udp_write
.ip
= priv
->remoteip
;
139 udp_write
.gw
= gateway(udp_write
.ip
);
140 udp_write
.src_port
= tid
;
141 udp_write
.dst_port
= socket
->tftp_remoteport
;
142 udp_write
.buffer_size
= len
;
144 pxe_call(PXENV_UDP_WRITE
, &udp_write
);
150 * Send a UDP packet to a destination
152 * @param:socket, the open socket
153 * @param:data, data buffer to send
154 * @param:len, size of data bufer
155 * @param:ip, the ip address
156 * @param:port, the port number, host-byte order
158 void core_udp_sendto(struct pxe_pvt_inode
*socket
, const void *data
, size_t len
,
159 uint32_t ip
, uint16_t port
)
161 static __lowmem
struct s_PXENV_UDP_WRITE udp_write
;
162 struct net_private_tftp
*priv
= &socket
->net
.tftp
;
170 memcpy(lbuf
, data
, len
);
172 tid
= priv
->localport
; /* TID(local port No) */
173 udp_write
.buffer
= FAR_PTR(lbuf
);
175 udp_write
.gw
= gateway(udp_write
.ip
);
176 udp_write
.src_port
= tid
;
177 udp_write
.dst_port
= htons(port
);
178 udp_write
.buffer_size
= len
;
180 pxe_call(PXENV_UDP_WRITE
, &udp_write
);
187 * Network stack-specific initialization
189 * Initialize UDP stack
191 void net_core_init(void)
194 static __lowmem
struct s_PXENV_UDP_OPEN udp_open
;
195 udp_open
.src_ip
= IPInfo
.myip
;
196 err
= pxe_call(PXENV_UDP_OPEN
, &udp_open
);
197 if (err
|| udp_open
.status
) {
198 printf("Failed to initialize UDP stack ");
199 printf("%d\n", udp_open
.status
);
204 void probe_undi(void)
208 void pxe_init_isr(void)
214 static __lowmem
struct s_PXENV_UDP_CLOSE udp_close
;
219 pxe_call(PXENV_UDP_CLOSE
, &udp_close
);