mirror of
https://github.com/openshwprojects/OpenBK7231T_App.git
synced 2026-02-20 00:32:37 +01:00
m
This commit is contained in:
@@ -106,7 +106,6 @@ static void store_sector(unsigned int addr, unsigned char *data){
|
||||
OTA_IncrementProgress(SECTOR_SIZE);
|
||||
}
|
||||
|
||||
#if ENABLE_SEND_POSTANDGET
|
||||
|
||||
httprequest_t httprequest;
|
||||
|
||||
|
||||
306
src/hal/bl602/hal_ota_bl602.c
Normal file
306
src/hal/bl602/hal_ota_bl602.c
Normal file
@@ -0,0 +1,306 @@
|
||||
|
||||
#include <hal_boot2.h>
|
||||
#include <utils_sha256.h>
|
||||
#include <bl_mtd.h>
|
||||
#include <bl_flash.h>
|
||||
#include "../../obk_config.h"
|
||||
#include "../../new_common.h"
|
||||
#include "lwip/sockets.h"
|
||||
|
||||
|
||||
typedef struct ota_header {
|
||||
union {
|
||||
struct {
|
||||
uint8_t header[16];
|
||||
|
||||
uint8_t type[4];//RAW XZ
|
||||
uint32_t len;//body len
|
||||
uint8_t pad0[8];
|
||||
|
||||
uint8_t ver_hardware[16];
|
||||
uint8_t ver_software[16];
|
||||
|
||||
uint8_t sha256[32];
|
||||
uint32_t unpacked_len;//full len
|
||||
} s;
|
||||
uint8_t _pad[512];
|
||||
} u;
|
||||
} ota_header_t;
|
||||
#define OTA_HEADER_SIZE (sizeof(ota_header_t))
|
||||
|
||||
static int _check_ota_header(ota_header_t *ota_header, uint32_t *ota_len, int *use_xz)
|
||||
{
|
||||
char str[33];//assume max segment size
|
||||
int i;
|
||||
|
||||
memcpy(str, ota_header->u.s.header, sizeof(ota_header->u.s.header));
|
||||
str[sizeof(ota_header->u.s.header)] = '\0';
|
||||
puts("[OTA] [HEADER] ota header is ");
|
||||
puts(str);
|
||||
puts("\r\n");
|
||||
|
||||
memcpy(str, ota_header->u.s.type, sizeof(ota_header->u.s.type));
|
||||
str[sizeof(ota_header->u.s.type)] = '\0';
|
||||
puts("[OTA] [HEADER] file type is ");
|
||||
puts(str);
|
||||
puts("\r\n");
|
||||
if (strstr(str, "XZ")) {
|
||||
*use_xz = 1;
|
||||
}
|
||||
else {
|
||||
*use_xz = 0;
|
||||
}
|
||||
|
||||
memcpy(ota_len, &(ota_header->u.s.len), 4);
|
||||
printf("[OTA] [HEADER] file length (exclude ota header) is %lu\r\n", *ota_len);
|
||||
|
||||
memcpy(str, ota_header->u.s.ver_hardware, sizeof(ota_header->u.s.ver_hardware));
|
||||
str[sizeof(ota_header->u.s.ver_hardware)] = '\0';
|
||||
puts("[OTA] [HEADER] ver_hardware is ");
|
||||
puts(str);
|
||||
puts("\r\n");
|
||||
|
||||
memcpy(str, ota_header->u.s.ver_software, sizeof(ota_header->u.s.ver_software));
|
||||
str[sizeof(ota_header->u.s.ver_software)] = '\0';
|
||||
puts("[OTA] [HEADER] ver_software is ");
|
||||
puts(str);
|
||||
puts("\r\n");
|
||||
|
||||
memcpy(str, ota_header->u.s.sha256, sizeof(ota_header->u.s.sha256));
|
||||
str[sizeof(ota_header->u.s.sha256)] = '\0';
|
||||
puts("[OTA] [HEADER] sha256 is ");
|
||||
for (i = 0; i < sizeof(ota_header->u.s.sha256); i++) {
|
||||
printf("%02X", str[i]);
|
||||
}
|
||||
puts("\r\n");
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int http_rest_post_flash(http_request_t* request, int startaddr, int maxaddr)
|
||||
{
|
||||
int total = 0;
|
||||
int towrite = request->bodylen;
|
||||
char* writebuf = request->bodystart;
|
||||
int writelen = request->bodylen;
|
||||
|
||||
ADDLOG_DEBUG(LOG_FEATURE_OTA, "OTA post len %d", request->contentLength);
|
||||
|
||||
int sockfd, i;
|
||||
int ret;
|
||||
struct hostent* hostinfo;
|
||||
uint8_t* recv_buffer;
|
||||
struct sockaddr_in dest;
|
||||
iot_sha256_context ctx;
|
||||
uint8_t sha256_result[32];
|
||||
uint8_t sha256_img[32];
|
||||
bl_mtd_handle_t handle;
|
||||
//init_ota(startaddr);
|
||||
|
||||
|
||||
#define OTA_PROGRAM_SIZE (512)
|
||||
int ota_header_found, use_xz;
|
||||
ota_header_t* ota_header = 0;
|
||||
|
||||
ret = bl_mtd_open(BL_MTD_PARTITION_NAME_FW_DEFAULT, &handle, BL_MTD_OPEN_FLAG_BACKUP);
|
||||
if (ret)
|
||||
{
|
||||
return http_rest_error(request, -20, "Open Default FW partition failed");
|
||||
}
|
||||
|
||||
recv_buffer = pvPortMalloc(OTA_PROGRAM_SIZE);
|
||||
|
||||
unsigned int buffer_offset, flash_offset, ota_addr;
|
||||
uint32_t bin_size, part_size, running_size;
|
||||
uint8_t activeID;
|
||||
HALPartition_Entry_Config ptEntry;
|
||||
|
||||
activeID = hal_boot2_get_active_partition();
|
||||
|
||||
printf("Starting OTA test. OTA bin addr is %p, incoming len %i\r\n", recv_buffer, writelen);
|
||||
|
||||
printf("[OTA] [TEST] activeID is %u\r\n", activeID);
|
||||
|
||||
if (hal_boot2_get_active_entries(BOOT2_PARTITION_TYPE_FW, &ptEntry))
|
||||
{
|
||||
printf("PtTable_Get_Active_Entries fail\r\n");
|
||||
vPortFree(recv_buffer);
|
||||
bl_mtd_close(handle);
|
||||
return http_rest_error(request, -20, "PtTable_Get_Active_Entries fail");
|
||||
}
|
||||
ota_addr = ptEntry.Address[!ptEntry.activeIndex];
|
||||
bin_size = ptEntry.maxLen[!ptEntry.activeIndex];
|
||||
part_size = ptEntry.maxLen[!ptEntry.activeIndex];
|
||||
running_size = ptEntry.maxLen[ptEntry.activeIndex];
|
||||
(void)part_size;
|
||||
/*XXX if you use bin_size is product env, you may want to set bin_size to the actual
|
||||
* OTA BIN size, and also you need to splilt XIP_SFlash_Erase_With_Lock into
|
||||
* serveral pieces. Partition size vs bin_size check is also needed
|
||||
*/
|
||||
printf("Starting OTA test. OTA size is %lu\r\n", bin_size);
|
||||
|
||||
printf("[OTA] [TEST] activeIndex is %u, use OTA address=%08x\r\n", ptEntry.activeIndex, (unsigned int)ota_addr);
|
||||
|
||||
printf("[OTA] [TEST] Erase flash with size %lu...", bin_size);
|
||||
hal_update_mfg_ptable();
|
||||
|
||||
//Erase in chunks, because erasing everything at once is slow and causes issues with http connection
|
||||
uint32_t erase_offset = 0;
|
||||
uint32_t erase_len = 0;
|
||||
while (erase_offset < bin_size)
|
||||
{
|
||||
erase_len = bin_size - erase_offset;
|
||||
if (erase_len > 0x10000)
|
||||
{
|
||||
erase_len = 0x10000; //Erase in 64kb chunks
|
||||
}
|
||||
bl_mtd_erase(handle, erase_offset, erase_len);
|
||||
printf("[OTA] Erased: %lu / %lu \r\n", erase_offset, erase_len);
|
||||
erase_offset += erase_len;
|
||||
rtos_delay_milliseconds(100);
|
||||
}
|
||||
printf("[OTA] Done\r\n");
|
||||
|
||||
if (request->contentLength >= 0)
|
||||
{
|
||||
towrite = request->contentLength;
|
||||
}
|
||||
|
||||
// get header
|
||||
// recv_buffer
|
||||
//buffer_offset = 0;
|
||||
//do {
|
||||
// int take_len;
|
||||
|
||||
// take_len = OTA_PROGRAM_SIZE - buffer_offset;
|
||||
|
||||
// memcpy(recv_buffer + buffer_offset, writebuf, writelen);
|
||||
// buffer_offset += writelen;
|
||||
|
||||
|
||||
// if (towrite > 0) {
|
||||
// writebuf = request->received;
|
||||
// writelen = recv(request->fd, writebuf, request->receivedLenmax, 0);
|
||||
// if (writelen < 0) {
|
||||
// ADDLOG_DEBUG(LOG_FEATURE_OTA, "recv returned %d - end of data - remaining %d", writelen, towrite);
|
||||
// }
|
||||
// }
|
||||
//} while(true)
|
||||
|
||||
buffer_offset = 0;
|
||||
flash_offset = 0;
|
||||
ota_header = 0;
|
||||
use_xz = 0;
|
||||
|
||||
utils_sha256_init(&ctx);
|
||||
utils_sha256_starts(&ctx);
|
||||
memset(sha256_result, 0, sizeof(sha256_result));
|
||||
do
|
||||
{
|
||||
char* useBuf = writebuf;
|
||||
int useLen = writelen;
|
||||
|
||||
if (ota_header == 0)
|
||||
{
|
||||
int take_len;
|
||||
|
||||
// how much left for header?
|
||||
take_len = OTA_PROGRAM_SIZE - buffer_offset;
|
||||
// clamp to available len
|
||||
if (take_len > useLen)
|
||||
take_len = useLen;
|
||||
printf("Header takes %i. ", take_len);
|
||||
memcpy(recv_buffer + buffer_offset, writebuf, take_len);
|
||||
buffer_offset += take_len;
|
||||
useBuf = writebuf + take_len;
|
||||
useLen = writelen - take_len;
|
||||
|
||||
if (buffer_offset >= OTA_PROGRAM_SIZE)
|
||||
{
|
||||
ota_header = (ota_header_t*)recv_buffer;
|
||||
if (strncmp((const char*)ota_header, "BL60X_OTA", 9))
|
||||
{
|
||||
return http_rest_error(request, -20, "Invalid header ident");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (ota_header && useLen)
|
||||
{
|
||||
|
||||
|
||||
if (flash_offset + useLen >= part_size)
|
||||
{
|
||||
return http_rest_error(request, -20, "Too large bin");
|
||||
}
|
||||
if (ota_header->u.s.unpacked_len != 0xFFFFFFFF && running_size < ota_header->u.s.unpacked_len)
|
||||
{
|
||||
ADDLOG_ERROR(LOG_FEATURE_OTA, "Unpacked OTA image size (%u) is bigger than running partition size (%u)", ota_header->u.s.unpacked_len, running_size);
|
||||
return http_rest_error(request, -20, "");
|
||||
}
|
||||
//ADDLOG_DEBUG(LOG_FEATURE_OTA, "%d bytes to write", writelen);
|
||||
//add_otadata((unsigned char*)writebuf, writelen);
|
||||
|
||||
printf("Flash takes %i. ", useLen);
|
||||
utils_sha256_update(&ctx, (byte*)useBuf, useLen);
|
||||
bl_mtd_write(handle, flash_offset, useLen, (byte*)useBuf);
|
||||
flash_offset += useLen;
|
||||
}
|
||||
|
||||
total += writelen;
|
||||
startaddr += writelen;
|
||||
towrite -= writelen;
|
||||
|
||||
|
||||
if (towrite > 0)
|
||||
{
|
||||
writebuf = request->received;
|
||||
writelen = recv(request->fd, writebuf, request->receivedLenmax, 0);
|
||||
if (writelen < 0)
|
||||
{
|
||||
ADDLOG_DEBUG(LOG_FEATURE_OTA, "recv returned %d - end of data - remaining %d", writelen, towrite);
|
||||
}
|
||||
}
|
||||
} while ((towrite > 0) && (writelen >= 0));
|
||||
|
||||
if (ota_header == 0)
|
||||
{
|
||||
return http_rest_error(request, -20, "No header found");
|
||||
}
|
||||
utils_sha256_finish(&ctx, sha256_result);
|
||||
puts("\r\nCalculated SHA256 Checksum:");
|
||||
for (i = 0; i < sizeof(sha256_result); i++)
|
||||
{
|
||||
printf("%02X", sha256_result[i]);
|
||||
}
|
||||
puts("\r\nHeader SHA256 Checksum:");
|
||||
for (i = 0; i < sizeof(sha256_result); i++)
|
||||
{
|
||||
printf("%02X", ota_header->u.s.sha256[i]);
|
||||
}
|
||||
if (memcmp(ota_header->u.s.sha256, sha256_result, sizeof(sha256_img)))
|
||||
{
|
||||
/*Error found*/
|
||||
return http_rest_error(request, -20, "SHA256 NOT Correct");
|
||||
}
|
||||
printf("[OTA] [TCP] prepare OTA partition info\r\n");
|
||||
ptEntry.len = total;
|
||||
printf("[OTA] [TCP] Update PARTITION, partition len is %lu\r\n", ptEntry.len);
|
||||
hal_boot2_update_ptable(&ptEntry);
|
||||
printf("[OTA] [TCP] Rebooting\r\n");
|
||||
//close_ota();
|
||||
vPortFree(recv_buffer);
|
||||
utils_sha256_free(&ctx);
|
||||
bl_mtd_close(handle);
|
||||
|
||||
ADDLOG_DEBUG(LOG_FEATURE_OTA, "%d total bytes written", total);
|
||||
http_setup(request, httpMimeTypeJson);
|
||||
hprintf255(request, "{\"size\":%d}", total);
|
||||
poststr(request, NULL);
|
||||
CFG_IncrementOTACount();
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
299
src/hal/w800/hal_ota_w800.c
Normal file
299
src/hal/w800/hal_ota_w800.c
Normal file
@@ -0,0 +1,299 @@
|
||||
#include "../obk_config.h"
|
||||
|
||||
#include "../new_common.h"
|
||||
#include "../logging/logging.h"
|
||||
#include "../httpserver/new_http.h"
|
||||
#include "../new_pins.h"
|
||||
#include "../jsmn/jsmn_h.h"
|
||||
#include "../hal/hal_ota.h"
|
||||
#include "../hal/hal_wifi.h"
|
||||
#include "../hal/hal_flashVars.h"
|
||||
#include "../littlefs/our_lfs.h"
|
||||
#include "lwip/sockets.h"
|
||||
|
||||
|
||||
#include "wm_internal_flash.h"
|
||||
#include "wm_socket_fwup.h"
|
||||
#include "wm_fwup.h"
|
||||
|
||||
int http_rest_post_flash(http_request_t* request, int startaddr, int maxaddr)
|
||||
{
|
||||
int total = 0;
|
||||
int towrite = request->bodylen;
|
||||
char* writebuf = request->bodystart;
|
||||
int writelen = request->bodylen;
|
||||
|
||||
ADDLOG_DEBUG(LOG_FEATURE_OTA, "OTA post len %d", request->contentLength);
|
||||
|
||||
#ifdef PLATFORM_W600
|
||||
int nRetCode = 0;
|
||||
char error_message[256];
|
||||
|
||||
if (writelen < 0)
|
||||
{
|
||||
ADDLOG_DEBUG(LOG_FEATURE_OTA, "ABORTED: %d bytes to write", writelen);
|
||||
return http_rest_error(request, -20, "writelen < 0");
|
||||
}
|
||||
|
||||
struct pbuf* p;
|
||||
|
||||
//Data is uploaded in 1024 sized chunks, creating a bigger buffer just in case this assumption changes.
|
||||
//The code below is based on sdk\OpenW600\src\app\ota\wm_http_fwup.c
|
||||
char* Buffer = (char*)os_malloc(2048 + 3);
|
||||
memset(Buffer, 0, 2048 + 3);
|
||||
|
||||
if (request->contentLength >= 0)
|
||||
{
|
||||
towrite = request->contentLength;
|
||||
}
|
||||
|
||||
int recvLen = 0;
|
||||
int totalLen = 0;
|
||||
//printf("\ntowrite %d writelen=%d\n", towrite, writelen);
|
||||
|
||||
do
|
||||
{
|
||||
if (writelen > 0)
|
||||
{
|
||||
//bk_printf("Copying %d from writebuf to Buffer towrite=%d\n", writelen, towrite);
|
||||
memcpy(Buffer + 3, writebuf, writelen);
|
||||
|
||||
if (recvLen == 0)
|
||||
{
|
||||
T_BOOTER* booter = (T_BOOTER*)(Buffer + 3);
|
||||
bk_printf("magic_no=%u, img_type=%u, zip_type=%u\n", booter->magic_no, booter->img_type, booter->zip_type);
|
||||
|
||||
if (TRUE == tls_fwup_img_header_check(booter))
|
||||
{
|
||||
totalLen = booter->upd_img_len + sizeof(T_BOOTER);
|
||||
OTA_ResetProgress();
|
||||
OTA_SetTotalBytes(totalLen);
|
||||
}
|
||||
else
|
||||
{
|
||||
sprintf(error_message, "Image header check failed");
|
||||
nRetCode = -19;
|
||||
break;
|
||||
}
|
||||
|
||||
nRetCode = socket_fwup_accept(0, ERR_OK);
|
||||
if (nRetCode != ERR_OK)
|
||||
{
|
||||
sprintf(error_message, "Firmware update startup failed");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
p = pbuf_alloc(PBUF_TRANSPORT, writelen + 3, PBUF_REF);
|
||||
if (!p)
|
||||
{
|
||||
sprintf(error_message, "Unable to allocate memory for buffer");
|
||||
nRetCode = -18;
|
||||
break;
|
||||
}
|
||||
|
||||
if (recvLen == 0)
|
||||
{
|
||||
*Buffer = SOCKET_FWUP_START;
|
||||
}
|
||||
else if (recvLen == (totalLen - writelen))
|
||||
{
|
||||
*Buffer = SOCKET_FWUP_END;
|
||||
}
|
||||
else
|
||||
{
|
||||
*Buffer = SOCKET_FWUP_DATA;
|
||||
}
|
||||
|
||||
*(Buffer + 1) = (writelen >> 8) & 0xFF;
|
||||
*(Buffer + 2) = writelen & 0xFF;
|
||||
p->payload = Buffer;
|
||||
p->len = p->tot_len = writelen + 3;
|
||||
|
||||
nRetCode = socket_fwup_recv(0, p, ERR_OK);
|
||||
if (nRetCode != ERR_OK)
|
||||
{
|
||||
sprintf(error_message, "Firmware data processing failed");
|
||||
break;
|
||||
}
|
||||
else
|
||||
{
|
||||
OTA_IncrementProgress(writelen);
|
||||
recvLen += writelen;
|
||||
printf("Downloaded %d / %d\n", recvLen, totalLen);
|
||||
}
|
||||
|
||||
towrite -= writelen;
|
||||
}
|
||||
|
||||
if (towrite > 0)
|
||||
{
|
||||
writebuf = request->received;
|
||||
writelen = recv(request->fd, writebuf, request->receivedLenmax, 0);
|
||||
if (writelen < 0)
|
||||
{
|
||||
sprintf(error_message, "recv returned %d - end of data - remaining %d", writelen, towrite);
|
||||
nRetCode = -17;
|
||||
}
|
||||
}
|
||||
} while ((nRetCode == 0) && (towrite > 0) && (writelen >= 0));
|
||||
|
||||
tls_mem_free(Buffer);
|
||||
|
||||
if (nRetCode != 0)
|
||||
{
|
||||
ADDLOG_ERROR(LOG_FEATURE_OTA, error_message);
|
||||
socket_fwup_err(0, nRetCode);
|
||||
return http_rest_error(request, nRetCode, error_message);
|
||||
}
|
||||
|
||||
|
||||
#elif PLATFORM_W800
|
||||
int nRetCode = 0;
|
||||
char error_message[256];
|
||||
|
||||
if (writelen < 0)
|
||||
{
|
||||
ADDLOG_ERROR(LOG_FEATURE_OTA, "ABORTED: %d bytes to write", writelen);
|
||||
return http_rest_error(request, -20, "writelen < 0");
|
||||
}
|
||||
|
||||
struct pbuf* p;
|
||||
|
||||
//The code below is based on W600 code and adopted to the differences in sdk\OpenW800\src\app\ota\wm_http_fwup.c
|
||||
// fiexd crashing caused by not checking "writelen" before doing memcpy
|
||||
// e.g. if more than 2 packets arrived before next loop, writelen could be > 2048 !!
|
||||
#define FWUP_MSG_SIZE 3
|
||||
#define MAX_BUFF_SIZE 2048
|
||||
char* Buffer = (char*)os_malloc(MAX_BUFF_SIZE + FWUP_MSG_SIZE);
|
||||
|
||||
if (!Buffer)
|
||||
{
|
||||
ADDLOG_ERROR(LOG_FEATURE_OTA, "ABORTED: failed to allocate buffer");
|
||||
return http_rest_error(request, -20, "");
|
||||
}
|
||||
|
||||
if (request->contentLength >= 0)
|
||||
{
|
||||
towrite = request->contentLength;
|
||||
}
|
||||
|
||||
int recvLen = 0;
|
||||
int totalLen = 0;
|
||||
printf("\ntowrite %d writelen=%d\n", towrite, writelen);
|
||||
|
||||
do
|
||||
{
|
||||
while (writelen > 0)
|
||||
{
|
||||
int actwrite = writelen < MAX_BUFF_SIZE ? writelen : MAX_BUFF_SIZE; // mustn't write more than Buffers size! Will crash else!
|
||||
//bk_printf("Copying %d from writebuf to Buffer (writelen=%d) towrite=%d -- free_heap:%d\n", actwrite, writelen, towrite, xPortGetFreeHeapSize());
|
||||
memset(Buffer, 0, MAX_BUFF_SIZE + FWUP_MSG_SIZE);
|
||||
memcpy(Buffer + FWUP_MSG_SIZE, writebuf, actwrite);
|
||||
if (recvLen == 0)
|
||||
{
|
||||
IMAGE_HEADER_PARAM_ST *booter = (IMAGE_HEADER_PARAM_ST*)(Buffer + FWUP_MSG_SIZE);
|
||||
bk_printf("magic_no=%u, img_type=%u, zip_type=%u, signature=%u\n",
|
||||
booter->magic_no, booter->img_attr.b.img_type, booter->img_attr.b.zip_type, booter->img_attr.b.signature);
|
||||
|
||||
if (TRUE == tls_fwup_img_header_check(booter))
|
||||
{
|
||||
totalLen = booter->img_len + sizeof(IMAGE_HEADER_PARAM_ST);
|
||||
if (booter->img_attr.b.signature)
|
||||
{
|
||||
totalLen += 128;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
sprintf(error_message, "Image header check failed");
|
||||
nRetCode = -19;
|
||||
break;
|
||||
}
|
||||
|
||||
nRetCode = socket_fwup_accept(0, ERR_OK);
|
||||
if (nRetCode != ERR_OK)
|
||||
{
|
||||
sprintf(error_message, "Firmware update startup failed");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
p = pbuf_alloc(PBUF_TRANSPORT, actwrite + FWUP_MSG_SIZE, PBUF_REF);
|
||||
if (!p)
|
||||
{
|
||||
sprintf(error_message, "Unable to allocate memory for buffer");
|
||||
nRetCode = -18;
|
||||
break;
|
||||
}
|
||||
|
||||
if (recvLen == 0)
|
||||
{
|
||||
*Buffer = SOCKET_FWUP_START;
|
||||
}
|
||||
else if (recvLen == (totalLen - actwrite))
|
||||
{
|
||||
*Buffer = SOCKET_FWUP_END;
|
||||
}
|
||||
else
|
||||
{
|
||||
*Buffer = SOCKET_FWUP_DATA;
|
||||
}
|
||||
|
||||
*(Buffer + 1) = (actwrite >> 8) & 0xFF;
|
||||
*(Buffer + 2) = actwrite & 0xFF;
|
||||
p->payload = Buffer;
|
||||
p->len = p->tot_len = actwrite + FWUP_MSG_SIZE;
|
||||
|
||||
nRetCode = socket_fwup_recv(0, p, ERR_OK);
|
||||
if (nRetCode != ERR_OK)
|
||||
{
|
||||
sprintf(error_message, "Firmware data processing failed");
|
||||
break;
|
||||
}
|
||||
else
|
||||
{
|
||||
recvLen += actwrite;
|
||||
}
|
||||
|
||||
towrite -= actwrite;
|
||||
writelen -= actwrite; // calculate, how much is left to write
|
||||
writebuf += actwrite; // in case, we only wrote part of buffer, advance in buffer
|
||||
}
|
||||
|
||||
if (towrite > 0)
|
||||
{
|
||||
writebuf = request->received;
|
||||
writelen = recv(request->fd, writebuf, request->receivedLenmax, 0);
|
||||
if (writelen < 0)
|
||||
{
|
||||
sprintf(error_message, "recv returned %d - end of data - remaining %d", writelen, towrite);
|
||||
nRetCode = -17;
|
||||
}
|
||||
}
|
||||
ADDLOG_DEBUG(LOG_FEATURE_OTA, "Downloaded %d / %d", recvLen, totalLen);
|
||||
rtos_delay_milliseconds(10); // give some time for flashing - will else increase used memory fast
|
||||
} while ((nRetCode == 0) && (towrite > 0) && (writelen >= 0));
|
||||
bk_printf("Download completed (%d / %d)\n", recvLen, totalLen);
|
||||
if (Buffer) os_free(Buffer);
|
||||
if (p) pbuf_free(p);
|
||||
|
||||
|
||||
if (nRetCode != 0)
|
||||
{
|
||||
ADDLOG_ERROR(LOG_FEATURE_OTA, error_message);
|
||||
socket_fwup_err(0, nRetCode);
|
||||
return http_rest_error(request, nRetCode, error_message);
|
||||
}
|
||||
|
||||
|
||||
|
||||
#endif
|
||||
ADDLOG_DEBUG(LOG_FEATURE_OTA, "%d total bytes written", total);
|
||||
http_setup(request, httpMimeTypeJson);
|
||||
hprintf255(request, "{\"size\":%d}", total);
|
||||
poststr(request, NULL);
|
||||
CFG_IncrementOTACount();
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -21,17 +21,6 @@
|
||||
uint32_t flash_read(uint32_t flash, uint32_t addr, void* buf, uint32_t size);
|
||||
#define FLASH_INDEX_XR809 0
|
||||
|
||||
#elif PLATFORM_BL602
|
||||
#include <hal_boot2.h>
|
||||
#include <utils_sha256.h>
|
||||
#include <bl_mtd.h>
|
||||
#include <bl_flash.h>
|
||||
|
||||
#elif defined(PLATFORM_W800) || defined(PLATFORM_W600)
|
||||
|
||||
#include "wm_internal_flash.h"
|
||||
#include "wm_socket_fwup.h"
|
||||
#include "wm_fwup.h"
|
||||
|
||||
#elif PLATFORM_LN882H
|
||||
|
||||
@@ -1370,77 +1359,7 @@ static int http_rest_error(http_request_t* request, int code, char* msg) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
#if PLATFORM_BL602
|
||||
|
||||
typedef struct ota_header {
|
||||
union {
|
||||
struct {
|
||||
uint8_t header[16];
|
||||
|
||||
uint8_t type[4];//RAW XZ
|
||||
uint32_t len;//body len
|
||||
uint8_t pad0[8];
|
||||
|
||||
uint8_t ver_hardware[16];
|
||||
uint8_t ver_software[16];
|
||||
|
||||
uint8_t sha256[32];
|
||||
uint32_t unpacked_len;//full len
|
||||
} s;
|
||||
uint8_t _pad[512];
|
||||
} u;
|
||||
} ota_header_t;
|
||||
#define OTA_HEADER_SIZE (sizeof(ota_header_t))
|
||||
|
||||
static int _check_ota_header(ota_header_t *ota_header, uint32_t *ota_len, int *use_xz)
|
||||
{
|
||||
char str[33];//assume max segment size
|
||||
int i;
|
||||
|
||||
memcpy(str, ota_header->u.s.header, sizeof(ota_header->u.s.header));
|
||||
str[sizeof(ota_header->u.s.header)] = '\0';
|
||||
puts("[OTA] [HEADER] ota header is ");
|
||||
puts(str);
|
||||
puts("\r\n");
|
||||
|
||||
memcpy(str, ota_header->u.s.type, sizeof(ota_header->u.s.type));
|
||||
str[sizeof(ota_header->u.s.type)] = '\0';
|
||||
puts("[OTA] [HEADER] file type is ");
|
||||
puts(str);
|
||||
puts("\r\n");
|
||||
if (strstr(str, "XZ")) {
|
||||
*use_xz = 1;
|
||||
}
|
||||
else {
|
||||
*use_xz = 0;
|
||||
}
|
||||
|
||||
memcpy(ota_len, &(ota_header->u.s.len), 4);
|
||||
printf("[OTA] [HEADER] file length (exclude ota header) is %lu\r\n", *ota_len);
|
||||
|
||||
memcpy(str, ota_header->u.s.ver_hardware, sizeof(ota_header->u.s.ver_hardware));
|
||||
str[sizeof(ota_header->u.s.ver_hardware)] = '\0';
|
||||
puts("[OTA] [HEADER] ver_hardware is ");
|
||||
puts(str);
|
||||
puts("\r\n");
|
||||
|
||||
memcpy(str, ota_header->u.s.ver_software, sizeof(ota_header->u.s.ver_software));
|
||||
str[sizeof(ota_header->u.s.ver_software)] = '\0';
|
||||
puts("[OTA] [HEADER] ver_software is ");
|
||||
puts(str);
|
||||
puts("\r\n");
|
||||
|
||||
memcpy(str, ota_header->u.s.sha256, sizeof(ota_header->u.s.sha256));
|
||||
str[sizeof(ota_header->u.s.sha256)] = '\0';
|
||||
puts("[OTA] [HEADER] sha256 is ");
|
||||
for (i = 0; i < sizeof(ota_header->u.s.sha256); i++) {
|
||||
printf("%02X", str[i]);
|
||||
}
|
||||
puts("\r\n");
|
||||
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
#if PLATFORM_LN882H
|
||||
#include "ota_port.h"
|
||||
@@ -1662,7 +1581,7 @@ static int ota_verify_download(void)
|
||||
}
|
||||
#endif
|
||||
|
||||
static int http_rest_post_flash(http_request_t* request, int startaddr, int maxaddr)
|
||||
int http_rest_post_flash(http_request_t* request, int startaddr, int maxaddr)
|
||||
{
|
||||
int total = 0;
|
||||
int towrite = request->bodylen;
|
||||
@@ -1671,269 +1590,7 @@ static int http_rest_post_flash(http_request_t* request, int startaddr, int maxa
|
||||
|
||||
ADDLOG_DEBUG(LOG_FEATURE_OTA, "OTA post len %d", request->contentLength);
|
||||
|
||||
#ifdef PLATFORM_W600
|
||||
int nRetCode = 0;
|
||||
char error_message[256];
|
||||
|
||||
if(writelen < 0)
|
||||
{
|
||||
ADDLOG_DEBUG(LOG_FEATURE_OTA, "ABORTED: %d bytes to write", writelen);
|
||||
return http_rest_error(request, -20, "writelen < 0");
|
||||
}
|
||||
|
||||
struct pbuf* p;
|
||||
|
||||
//Data is uploaded in 1024 sized chunks, creating a bigger buffer just in case this assumption changes.
|
||||
//The code below is based on sdk\OpenW600\src\app\ota\wm_http_fwup.c
|
||||
char* Buffer = (char*)os_malloc(2048 + 3);
|
||||
memset(Buffer, 0, 2048 + 3);
|
||||
|
||||
if(request->contentLength >= 0)
|
||||
{
|
||||
towrite = request->contentLength;
|
||||
}
|
||||
|
||||
int recvLen = 0;
|
||||
int totalLen = 0;
|
||||
//printf("\ntowrite %d writelen=%d\n", towrite, writelen);
|
||||
|
||||
do
|
||||
{
|
||||
if(writelen > 0)
|
||||
{
|
||||
//bk_printf("Copying %d from writebuf to Buffer towrite=%d\n", writelen, towrite);
|
||||
memcpy(Buffer + 3, writebuf, writelen);
|
||||
|
||||
if(recvLen == 0)
|
||||
{
|
||||
T_BOOTER* booter = (T_BOOTER*)(Buffer + 3);
|
||||
bk_printf("magic_no=%u, img_type=%u, zip_type=%u\n", booter->magic_no, booter->img_type, booter->zip_type);
|
||||
|
||||
if(TRUE == tls_fwup_img_header_check(booter))
|
||||
{
|
||||
totalLen = booter->upd_img_len + sizeof(T_BOOTER);
|
||||
OTA_ResetProgress();
|
||||
OTA_SetTotalBytes(totalLen);
|
||||
}
|
||||
else
|
||||
{
|
||||
sprintf(error_message, "Image header check failed");
|
||||
nRetCode = -19;
|
||||
break;
|
||||
}
|
||||
|
||||
nRetCode = socket_fwup_accept(0, ERR_OK);
|
||||
if(nRetCode != ERR_OK)
|
||||
{
|
||||
sprintf(error_message, "Firmware update startup failed");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
p = pbuf_alloc(PBUF_TRANSPORT, writelen + 3, PBUF_REF);
|
||||
if(!p)
|
||||
{
|
||||
sprintf(error_message, "Unable to allocate memory for buffer");
|
||||
nRetCode = -18;
|
||||
break;
|
||||
}
|
||||
|
||||
if(recvLen == 0)
|
||||
{
|
||||
*Buffer = SOCKET_FWUP_START;
|
||||
}
|
||||
else if(recvLen == (totalLen - writelen))
|
||||
{
|
||||
*Buffer = SOCKET_FWUP_END;
|
||||
}
|
||||
else
|
||||
{
|
||||
*Buffer = SOCKET_FWUP_DATA;
|
||||
}
|
||||
|
||||
*(Buffer + 1) = (writelen >> 8) & 0xFF;
|
||||
*(Buffer + 2) = writelen & 0xFF;
|
||||
p->payload = Buffer;
|
||||
p->len = p->tot_len = writelen + 3;
|
||||
|
||||
nRetCode = socket_fwup_recv(0, p, ERR_OK);
|
||||
if(nRetCode != ERR_OK)
|
||||
{
|
||||
sprintf(error_message, "Firmware data processing failed");
|
||||
break;
|
||||
}
|
||||
else
|
||||
{
|
||||
OTA_IncrementProgress(writelen);
|
||||
recvLen += writelen;
|
||||
printf("Downloaded %d / %d\n", recvLen, totalLen);
|
||||
}
|
||||
|
||||
towrite -= writelen;
|
||||
}
|
||||
|
||||
if(towrite > 0)
|
||||
{
|
||||
writebuf = request->received;
|
||||
writelen = recv(request->fd, writebuf, request->receivedLenmax, 0);
|
||||
if(writelen < 0)
|
||||
{
|
||||
sprintf(error_message, "recv returned %d - end of data - remaining %d", writelen, towrite);
|
||||
nRetCode = -17;
|
||||
}
|
||||
}
|
||||
} while((nRetCode == 0) && (towrite > 0) && (writelen >= 0));
|
||||
|
||||
tls_mem_free(Buffer);
|
||||
|
||||
if(nRetCode != 0)
|
||||
{
|
||||
ADDLOG_ERROR(LOG_FEATURE_OTA, error_message);
|
||||
socket_fwup_err(0, nRetCode);
|
||||
return http_rest_error(request, nRetCode, error_message);
|
||||
}
|
||||
|
||||
|
||||
#elif PLATFORM_W800
|
||||
int nRetCode = 0;
|
||||
char error_message[256];
|
||||
|
||||
if(writelen < 0)
|
||||
{
|
||||
ADDLOG_ERROR(LOG_FEATURE_OTA, "ABORTED: %d bytes to write", writelen);
|
||||
return http_rest_error(request, -20, "writelen < 0");
|
||||
}
|
||||
|
||||
struct pbuf* p;
|
||||
|
||||
//The code below is based on W600 code and adopted to the differences in sdk\OpenW800\src\app\ota\wm_http_fwup.c
|
||||
// fiexd crashing caused by not checking "writelen" before doing memcpy
|
||||
// e.g. if more than 2 packets arrived before next loop, writelen could be > 2048 !!
|
||||
#define FWUP_MSG_SIZE 3
|
||||
#define MAX_BUFF_SIZE 2048
|
||||
char* Buffer = (char*)os_malloc(MAX_BUFF_SIZE + FWUP_MSG_SIZE);
|
||||
|
||||
if(!Buffer)
|
||||
{
|
||||
ADDLOG_ERROR(LOG_FEATURE_OTA, "ABORTED: failed to allocate buffer");
|
||||
return http_rest_error(request, -20, "");
|
||||
}
|
||||
|
||||
if(request->contentLength >= 0)
|
||||
{
|
||||
towrite = request->contentLength;
|
||||
}
|
||||
|
||||
int recvLen = 0;
|
||||
int totalLen = 0;
|
||||
printf("\ntowrite %d writelen=%d\n", towrite, writelen);
|
||||
|
||||
do
|
||||
{
|
||||
while(writelen > 0)
|
||||
{
|
||||
int actwrite = writelen < MAX_BUFF_SIZE ? writelen : MAX_BUFF_SIZE; // mustn't write more than Buffers size! Will crash else!
|
||||
//bk_printf("Copying %d from writebuf to Buffer (writelen=%d) towrite=%d -- free_heap:%d\n", actwrite, writelen, towrite, xPortGetFreeHeapSize());
|
||||
memset(Buffer, 0, MAX_BUFF_SIZE + FWUP_MSG_SIZE);
|
||||
memcpy(Buffer + FWUP_MSG_SIZE, writebuf, actwrite);
|
||||
if(recvLen == 0)
|
||||
{
|
||||
IMAGE_HEADER_PARAM_ST *booter = (IMAGE_HEADER_PARAM_ST*)(Buffer + FWUP_MSG_SIZE);
|
||||
bk_printf("magic_no=%u, img_type=%u, zip_type=%u, signature=%u\n",
|
||||
booter->magic_no, booter->img_attr.b.img_type, booter->img_attr.b.zip_type, booter->img_attr.b.signature);
|
||||
|
||||
if(TRUE == tls_fwup_img_header_check(booter))
|
||||
{
|
||||
totalLen = booter->img_len + sizeof(IMAGE_HEADER_PARAM_ST);
|
||||
if (booter->img_attr.b.signature)
|
||||
{
|
||||
totalLen += 128;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
sprintf(error_message, "Image header check failed");
|
||||
nRetCode = -19;
|
||||
break;
|
||||
}
|
||||
|
||||
nRetCode = socket_fwup_accept(0, ERR_OK);
|
||||
if(nRetCode != ERR_OK)
|
||||
{
|
||||
sprintf(error_message, "Firmware update startup failed");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
p = pbuf_alloc(PBUF_TRANSPORT, actwrite + FWUP_MSG_SIZE, PBUF_REF);
|
||||
if(!p)
|
||||
{
|
||||
sprintf(error_message, "Unable to allocate memory for buffer");
|
||||
nRetCode = -18;
|
||||
break;
|
||||
}
|
||||
|
||||
if(recvLen == 0)
|
||||
{
|
||||
*Buffer = SOCKET_FWUP_START;
|
||||
}
|
||||
else if(recvLen == (totalLen - actwrite))
|
||||
{
|
||||
*Buffer = SOCKET_FWUP_END;
|
||||
}
|
||||
else
|
||||
{
|
||||
*Buffer = SOCKET_FWUP_DATA;
|
||||
}
|
||||
|
||||
*(Buffer + 1) = (actwrite >> 8) & 0xFF;
|
||||
*(Buffer + 2) = actwrite & 0xFF;
|
||||
p->payload = Buffer;
|
||||
p->len = p->tot_len = actwrite + FWUP_MSG_SIZE;
|
||||
|
||||
nRetCode = socket_fwup_recv(0, p, ERR_OK);
|
||||
if(nRetCode != ERR_OK)
|
||||
{
|
||||
sprintf(error_message, "Firmware data processing failed");
|
||||
break;
|
||||
}
|
||||
else
|
||||
{
|
||||
recvLen += actwrite;
|
||||
}
|
||||
|
||||
towrite -= actwrite;
|
||||
writelen -= actwrite; // calculate, how much is left to write
|
||||
writebuf += actwrite; // in case, we only wrote part of buffer, advance in buffer
|
||||
}
|
||||
|
||||
if(towrite > 0)
|
||||
{
|
||||
writebuf = request->received;
|
||||
writelen = recv(request->fd, writebuf, request->receivedLenmax, 0);
|
||||
if(writelen < 0)
|
||||
{
|
||||
sprintf(error_message, "recv returned %d - end of data - remaining %d", writelen, towrite);
|
||||
nRetCode = -17;
|
||||
}
|
||||
}
|
||||
ADDLOG_DEBUG(LOG_FEATURE_OTA, "Downloaded %d / %d", recvLen, totalLen);
|
||||
rtos_delay_milliseconds(10); // give some time for flashing - will else increase used memory fast
|
||||
} while((nRetCode == 0) && (towrite > 0) && (writelen >= 0));
|
||||
bk_printf("Download completed (%d / %d)\n", recvLen, totalLen);
|
||||
if(Buffer) os_free(Buffer);
|
||||
if(p) pbuf_free(p);
|
||||
|
||||
|
||||
if(nRetCode != 0)
|
||||
{
|
||||
ADDLOG_ERROR(LOG_FEATURE_OTA, error_message);
|
||||
socket_fwup_err(0, nRetCode);
|
||||
return http_rest_error(request, nRetCode, error_message);
|
||||
}
|
||||
|
||||
|
||||
#elif PLATFORM_BL602
|
||||
#if PLATFORM_BL602
|
||||
int sockfd, i;
|
||||
int ret;
|
||||
struct hostent* hostinfo;
|
||||
|
||||
Reference in New Issue
Block a user