.
Dependencies: mbed EthernetInterface mbed-rtos
Fork of Bootloader_K64F by
Diff: Bootloader/bootloader.h
- Revision:
- 9:56ed3a56ecc3
- Parent:
- 8:00cefe0d59ed
--- a/Bootloader/bootloader.h Sat Apr 23 18:24:07 2016 +0000 +++ b/Bootloader/bootloader.h Sun Apr 24 12:46:10 2016 +0000 @@ -8,24 +8,17 @@ #define BUFFER_SIZE 1024 -//Bootloader functions +/*************************************************************/ +// Bootloader functions +/*************************************************************/ int *(*program_flash_boot)(int, char*, unsigned int) = (int *(*)(int, char*, unsigned int))0x795C9; int *(*erase_sector_boot)(int) = (int *(*)(int))0x79465; void *(*bootloader)(int) = (void *(*)(int))0x79121; -static void write(char *value) -{ - int i = 0; - //Loop through string and send everything - while(*(value+i) != '\0') { - while(!(UART0->S1 & UART_S1_TDRE_MASK)); - UART0->D = *(value+i); - i++; - } -} - -//Getting the new binary +/*************************************************************/ +// Retrieving binary from server +/*************************************************************/ void write_flash(void) { @@ -38,68 +31,100 @@ eth.init(); //Use DHCP eth.connect(); printf("IP Address is %s\r\n", eth.getIPAddress()); - + + TCPSocketConnection sock; + + //----------------YOUR SERVER DETAILS-----------------------// sock.connect("192.168.0.18", 80); - printf("Connected to mbed\r\n"); - char http_cmd[] = "HEAD /loader_test.bin HTTP/1.1\r\nHost: 192.168.0.18 80\r\n\r\n"; + char http_cmd[] = "GET /loader_test.bin HTTP/1.1\r\nHost: 192.168.0.18 80\r\n\r\n"; + //----------------YOUR SERVER DETAILS-----------------------// + sock.send_all(http_cmd, sizeof(http_cmd)-1); - printf("Send head request\r\n"); - + char buffer[BUFFER_SIZE]; - printf("Receiving head (hihi)\r\n"); - int headlength = sock.receive(buffer, sizeof(buffer)); - if (headlength == BUFFER_SIZE) { - printf("Head exceeded expected maximum, loading will fail\r\n"); - while(1); - } - sock.close(); - wait(2); - sock.connect("192.168.0.18", 80); - - char http_cmd2[] = "GET /loader_test.bin HTTP/1.1\r\nHost: 192.168.0.18 80\r\n\r\n"; - - sock.send_all(http_cmd2, sizeof(http_cmd2)-1); - printf("Send get request\r\n"); - if (sock.receive(buffer, headlength) != headlength) { - printf("No response\r\n"); + int received; + int binsize; + int bufcount; + int remaining; + int count = 0; + + //Receive first packet + received = sock.receive(buffer, sizeof(buffer)); + if (received <= 0) { + printf("No data received from server\r\n"); while(1); } + + //Search for "Content-Length", if not available, receive more until buffer is full + while(strstr(buffer, "Content-Length: ") == 0) { + if (received == sizeof(buffer)) { + printf("Could not determine size of bin file\r\n"); + while(1); + } else { + received += sock.receive(buffer + received, sizeof(buffer) - received); + } + } + //Determine size of the file + char *temp = strstr(buffer, "Content-Length: ") + 16; //'16' is size of "Content-Length: " + sscanf(temp, "%d", &binsize); + printf("Size of the binary = %d bytes\r\n", binsize); - int count; - int ret; - int received = 0; - while (true) { - //printf("Receiving\r\n"); - ret = sock.receive(&buffer[received], 16-received); - received = received + ret; - if (ret <= 0) { - if (received != 0) { - program_flash_boot(count+START_ADDRESS, buffer, 16); - } - break; + //Search for "\r\n\r\n" (beginning of bin file), if not available, receive more until buffer is full + while(strstr(buffer, "\r\n\r\n") == 0) { + if (received == sizeof(buffer)) { + printf("Could not find start of bin file\r\n"); + while(1); + } else { + received += sock.receive(buffer+received, sizeof(buffer) - received); } + } + //Get pointer to begin of the file in the buffer + temp = strstr(buffer, "\r\n\r\n") + 4; //'16' is size of "\r\n\r\n" + + //See how much of the bin file we already received, and move this to the start of the buffer + bufcount = received - ((uint32_t)temp - (uint32_t)buffer); + memmove(buffer, temp, bufcount); + printf("Received %d bytes\r\n", bufcount); + + //Start receiving the remaining bin file + remaining = binsize - bufcount; - printf("%d\r\n", ret); - if (received == 16) { - __disable_irq(); - if (program_flash_boot(count+START_ADDRESS, buffer, 16) != 0) { - write("Error!\r\n"); + while (remaining > 0) { + //Completely fill the buffer each time so we can easily write it to flash + while (bufcount < sizeof(buffer)) { + //Try to receive remainder of the buffer + received = sock.receive(&buffer[bufcount], sizeof(buffer)-bufcount); + printf("Received %d\r\n", received); + if (received <= 0) { + printf("Error, should not happen\r\n"); while(1); } - __enable_irq(); - count += received; - received = 0; + //Track how much we received and how much is left + bufcount += received; + remaining -= received; + if (remaining == 0) { + if (program_flash_boot(count+START_ADDRESS, buffer, sizeof(buffer)) != 0) { + printf("Error @ 0x%X!\r\n", count); + while(1); + } + count += sizeof(buffer); + break; + } } - - + //Buffer is full, program it and increase the counter (the counter is a bit redundant, we could get it from the other variables) + if (program_flash_boot(count+START_ADDRESS, buffer, sizeof(buffer)) != 0) { + printf("Error @ 0x%X!\r\n", count); + while(1); + } + count += sizeof(buffer); + bufcount = 0; } - + printf("Done\r\n"); sock.close(); - + eth.disconnect(); - printf("Done\r\n"); bootloader(count);