Recently need to implement through the TCP/IP remote IAP Online update function, 2 weeks to finally in the original embedded server based on the implementation of this function, here to record the implementation of the process.
IAP is also called in the application programming, in fact, the simple point is that the implementation does not need to jlink, only through the chip with an interface such as can,usb,ethernet can be downloaded. Take the stm32f207 chip I used for example, there are three kinds of starting mode, SRAM start, User boot ( That is, flash address start, user application execution), system boot (that is, the systems address to start, for serial port download), see if this understand something, the System boot mode download implementation process is the IAP application programming, but this program is generally the chip company burned in the internal fixed address (modifications are generally not allowed). In this case, the network remote download update is also the implementation of this process, but the execution will be completely in user boot (that is, Flash). above is the concept of IAP, the following into the topic: Step One: Learn about ARM's startup process (This web has a very clear explanation, here is a cursory), power on, Reset, STM32 chip based on the boot pin to the interrupt vector table address (the starting address) placed 0x80000000, while the PC pointer at the address 0x80000000 ( Here the 0x80000000 is mapped by the Flash address 0x08000000), that is, the start is to jump to the first flash address, then the completion is to build the stack, and finally jump to the _main function (startup file, Of course stm32f2xx's header file is first jumped to systeminit), at this time the program is formally executed. In fact, understand the above knowledge, the implementation of the IAP is better understood (because I actually operate with the STM32 provided by the scheme (see STM32F2X7_ETH_IAP) There is a difference, here first to stm32 provide the scheme to do introduction), first of all must have the ability to implement the download program ( The so-called Boot guidance Program) and the user actually executes the program, after preliminary design, in the flash structure such as:
Where the guidance zone is used to implement the upgrade boot, user application is the code that users actually run. Plan the code in the entire flash address, the following is the specific implementation. The guidance program is the core program of the whole IAP implementation, it needs to implement two functions:
(1) In general, jump user application, execute code
(2) Under special circumstances to enter the IAP mode, you can update the user code (usually the button, of course, can also receive external instructions to enter IAP mode)
1. Jump Code
Official routines have standard code can be used directly, for example, it is to do is to jump the PC pointer to 0x08008000, the user application at the beginning of the code execution, note in the user The main function of the application program needs to relocate the interrupt vector table to the user application first address, that is, to add nvic_setvectortable (Nvic_vecttab_flash, 0x08008000); (General Nvic initialization will be redefined to 0x0, note the comment out) otherwise the application code interrupts will not respond, which is related to the mechanism of interrupt response, the default address is 0x0, when the interrupt is generated when the lookup vector table obtained address is Guidance program interrupt corresponding address, execution will be error.
2. Remote Network IAP Implementation
The implementation of the remote network IAP is based on LWIP, because the network interface layer in the LWIP, the configuration of the IP,TCP layer and the normal network communication is not different, and the knowledge is complex, here is not to repeat, mainly about the HTTP Layer Code processing. HTTP Layer code processing consists of two parts,
Handling of the 1.http header
2. Processing of body code (i.e. processing of received data)
Take the official example, the above two parts of the processing are implemented in the HTTP_RECV function, want to understand the actual process of HTTP send, then through packet capture analysis is the most concise way, here is the use of Wireshark software.
1. is the HTTP header that is sent when the browser performs the upgrade, since the first format is known, the receiving data can be processed on the server, and the data in the box above is the field that needs to be processed. First, the data that is received by the TCP layer is stored
Data, post/upload.cgi for HTTP contains the way HTTP, url, then by comparing strncmp (data, "post/upload.cgi", 16) = = 0 can determine whether to enter the upgrade (CGI) mode. Content-length is the length of the body because it is transmitted in characters, so the received characters are converted to numbers, and the routines are implemented through Parse_content_length. Of course, you can also use the STRSTR function to get the body length, the code is as follows:/* Read Content-length's first address */
if " ")) = NULL) { = &contentlengthstart[];}
Get PTR is content-length: After the first address of the numeric character, you can later convert the character array into a number, see the sample program, not repeat.
2. After the first processing is complete, the following is the HTTP text processing. Processing the HTTP body, of course, also need to analyze the packet sent by the browser, in case HTTP send the starting packet (728 bytes)
HTTP send starter Package actual content
Compare with the above header to know the first received packet 728 bytes contains only the HTTP header, where the end of the 0d 0a 0d 0a is \r\n\r\n (rest), that is determined as upload.cgi, modify the flag bit (uploadsymbol), Unlock Flash, Erase the specified address of Flash (erase sector2,3,4 in this case), get the body length can jump out, waiting for the next packet to receive, the following really begin the processing of the body.
How to deal with the text, or to send the actual data packet as a quasi-judgment, where the first and last to special processing, of course, this is also based on the actual transmission of data packets, as follows.
Main Page:
Body End:
See the above two data messages, you can clearly know that the real program starts from 87. I start the line, the end of the program to community the end of the line, of course, the red part of the whole is contained in the body data, which requires us to write flash to remove the data appended to the information, because it is more important, I put my processing code:
Char*httpheadendstart;//Header End AddressChar*postcontent;//Text First addressChar*pstr;inttotalreceived =0;//Incoming PacketsintLen//the actual length of the packet receivedtotalreceived+= p->tot_len;//The following parameters are the total length of packets received each timelen= p->Tot_len;if(httpheadendstart= strstr (data,"\r\n\r\n"))!=NULL) { if(httpheadendstart[4]) {postcontent= & httpheadendstart[4];//get the first text address}}pstr=postcontent;if(uploadsymbol==1)//the first packet received by the body{ if(Pstr = Strstr (Pstr,"\r\n\r\n")) = NULL)//Remove HTTP Additional information{pstr= &pstr[4];//87. Beginning AddressLen = Len-(PSTR-PSL);//minus the length of the additional information, that is, the actual data length }}if(totalreceived== contentlength)//at the end of the removal of HTTP additional information, the body receives the last packet{ if(PSL = Strstr (PSL,"\r\n--"))!=NULL) {Len= PSL-pstr; } If (len) {iap_http_writedata (Pstr,len);//write data to Flashtotalreceived =0; Uploaddatesymbol=0; Flash_lock (); }} Else{If (len) iap_http_writedata (Pstr, Len);//write data to Flash} uploadsymbol++;
As above, the entire data is completely written to flash, of course, the actual work far more than that, such as flash unlock, write and lock, where if the following 4 bytes of Word write, but also to consider the received packet is not a multiple of 4, then need to take out data from subsequent packets to fill 4 bytes, This part of the code is implemented by the Iap_http_writedata function, the method is very clever, want to understand the person can also interpret this part of the code. The above is mainly the normal operation of the data how to deal with, but there is a saying, good code is the wrong operation should also be given the appropriate response, then the customer if not upload the file on the Point upgrade button what to do, of course, or the old way, first grab packet analysis, in the solution, grab package diagram as follows:
See and above what difference does not, filename= "" in the middle is empty, then is good to do, here I do not give my own treatment method, the routine has:
for(i=0; i<len;i++){ if(STRNCMP (Char*) (Data+i),"filename=",9)==0) {Filenameoffset= i+Ten;//get Filename= "after character offset Break; }} I=0;if(filenameoffset) { while((* (* (data+filenameoffset + i)! =0x22) && (i< -))//It's easy to see the ASCII table. 0x22 represents the character "{Filename[i]= * (Data+filenameoffset +i); I++;} Filename[i]= ' \0';}
If i = = 0 jumps out, indicating that the file is not received, send an error file, or jump directly to the current page can be (specific to see the routine). This is just the code in the routine, in fact this paragraph can also be extended to upload only the specified file name and type of files, by modifying i==0 to STRNCMP ("filename", "Landtiger.bin", 14)! = 0 to specify only upload Landtiger.bin file, while the length can also be limited, so that upload files can only be in the specified size, these need to personally experience to have a harvest.
LWIP Implement network remote IAP download update