How to add a custom network communication in U-boot

Source: Internet
Author: User

U-boot does not have a TCP protocol stack and does not support TCP(proposed to support TCP-based in u-boot The agreement of the project manager you give me Out). But UDP is still there. Using u-boot with UDP can do many of the underlying functions. Even we plan to use u-boot to burn nand-flash in production .

This u-boot Tutorial does not have any reference material, completely looks at the u-boot code then writes out .

Preparing a software program for Netloop

To add a UDP processing tool to U-boot, you should first prepare the . C and for this program. H file. Like I wrote amc_udp.c and amc_udp.h .

Preparing the UDP send function

How to build send functions and interfaces depends on the requirements. But ultimately, this u-boot API needs to be called within the function:

Netsendudppacket (Uchar *ether,

ipaddr_t dest,

int Dport,

int sport,

int len);

The following is a description of each parameter:

Ether: The Ethernet address of the destination. If it is broadcast, the global variable netbroadcastaddris used,and if unknown, the char[6]={0,0,0,0,0,0} of all zeros is specified.

Dest: The IP address of the destination . Specify 0If it is a broadcast, ora ulong value if unicast. You can specify a global variable that is already set Netserverip

Dport: Destination port for packet sending, i.e. remote port

Sport: Not "Sport", but "Source-port". Source port for package send

Len: The length of the UDP packet body. Before sending, you need to set the text in advance, see below for instructions.

The above mentioned the text must be set before sending, then where is the text? The body is in a global variable of net.c, which should be obtained as follows:

Uchar *context = (Uchar *) (Nettxpacket + netethhdrsize () + ip_hdr_size);

Preparing The handler function for UDP Timeout

The timeout function looks like static void Amc_udp_timeout (). Mainly do some hyper-times need to process and then set

Netstate = Netloop_fail

Then return, so that netloop will prompt for an error to exit.

Prepare to receive handler function

The receive function forms such as:

static void Amc_udp_handler (Uchar *pkt, unsigned dest, unsigned srt, unsigned len);

The following lists the information that can be obtained in the function:

IP Header:
ip_t *ippky = pkt-(ip_hdr_size);
This sentence pushes the body of the PKT represented by a distance to obtain the IP header, at which point you can get a complete message, such as:
ipaddr_t Ipfrom = ippkg->ip_src; can be used with UDP response
For some other information, see The format of the IP message.

Port information:
The Dest indicates the destination Port, which is the port to which the remote is destined,and, correspondingly,thesrc represents the remote Port.

IP Message text:
The PKT itself is, using len to get the length

Finish processing:netloop The completion of processing is to see the global variable netstate . The general setting of netloop_fail or netloop_success leads to the end of Netloop () . Others have not been studied for the time being.

Preparing the Netloop () Call Interface

Write a function such as void Amc_udp_start () to begin the entire function. Before you begin, you need the following two sentences:

Netsethandler (Amc_udp_handler);

Netsettimeout (AMC_UDP_TIMEOUT_SEC * cfg_hz, amc_udp_timeout);

Once you have configured the handler and timeout callbacks, you can send UDP .

Another: During transmission, it is recommended to set netbootfilexfersize to represent the transfer size, that is, The return value of Netloop.

Modify net.c and net.h

First , add a protocol name to the net.h , "AMCUDP"as follows:

typedef enum {

BOOTP, RARP, ARP, TFTP, DHCP, PING, DNS, NFS,

CDP, Netcons, SNTP, AMCUDP

} proto_t;

Then add the Execution branch in the netloop :

......

Switch (protocol) {

Case TFTP:

...

Break

......

Case AMCUDP:

Amc_udp_start ();

Break

Default

Break

}

......

The back u-boot will naturally handle the network reception and the corresponding action according to the function you write.

Modify UDP Checksum

search in net.c for if (0 = = strcmp (getenv ("Udpsum"), "on"), or direct -search udpsum You can find The switch for UDP checksum. It is recommended to force open instead.

Some of the u-boot are written like this:

Ip->xsum = ~netcksum ((uchar *) IP, IP_HDR_SIZE_NO_UDP/2);

To start a custom network communication

The Netloop () function has very few incoming parameters, so it is often necessary to use a different method / function or a global variable to configure. After the configuration is complete, start netloop (). The method to invoke Netloop listener is:

Netloopret = Netloop (AMCUDP);

A return value of less than 0 represents a failure and can be retried

About ARP

In theory, when calling Netsendudppacket , if the incoming MAC address is full 0 , U-boot is automatically completed ARP process and then send out the defined ARP package. But the u-boot Code of my project is not like this, it just get the MAC address and then do nothing, this could be an unfinished bug .

Originally from:segmentfault

How to add a custom network communication in U-boot

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.