2015-06-30 21:11:15 +01:00
|
|
|
/*
|
|
|
|
* This file is part of the Micro Python project, http://micropython.org/
|
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
|
|
|
* Copyright (c) 2015 Daniel Campora
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2015-02-06 14:35:48 +00:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
|
2015-02-21 18:58:43 +00:00
|
|
|
#include "py/mpconfig.h"
|
2015-05-23 19:52:42 +01:00
|
|
|
#include "py/obj.h"
|
2015-02-06 14:35:48 +00:00
|
|
|
#include "simplelink.h"
|
|
|
|
#include "flc.h"
|
|
|
|
#include "updater.h"
|
|
|
|
#include "shamd5.h"
|
2015-05-23 19:52:42 +01:00
|
|
|
#include "modnetwork.h"
|
2015-02-06 14:35:48 +00:00
|
|
|
#include "modwlan.h"
|
|
|
|
#include "debug.h"
|
2015-07-14 20:39:07 +01:00
|
|
|
#include "osi.h"
|
2015-02-06 14:35:48 +00:00
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
DEFINE PRIVATE CONSTANTS
|
|
|
|
******************************************************************************/
|
2015-04-10 20:02:07 +01:00
|
|
|
#define UPDATER_IMG_PATH "/flash/sys/mcuimg.bin"
|
|
|
|
#define UPDATER_SRVPACK_PATH "/flash/sys/servicepack.ucf"
|
|
|
|
#define UPDATER_SIGN_PATH "/flash/sys/servicepack.sig"
|
2015-06-30 21:11:15 +01:00
|
|
|
#define UPDATER_CA_PATH "/flash/cert/ca.pem"
|
|
|
|
#define UPDATER_CERT_PATH "/flash/cert/cert.pem"
|
|
|
|
#define UPDATER_KEY_PATH "/flash/cert/private.key"
|
2015-02-06 14:35:48 +00:00
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
DEFINE TYPES
|
|
|
|
******************************************************************************/
|
|
|
|
typedef struct {
|
|
|
|
char *path;
|
|
|
|
_i32 fhandle;
|
|
|
|
_u32 fsize;
|
|
|
|
_u32 foffset;
|
|
|
|
} updater_data_t;
|
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
DECLARE PRIVATE DATA
|
|
|
|
******************************************************************************/
|
2015-07-14 20:39:07 +01:00
|
|
|
static updater_data_t updater_data = { .path = NULL, .fhandle = -1, .fsize = 0, .foffset = 0 };
|
|
|
|
static OsiLockObj_t updater_LockObj;
|
|
|
|
static sBootInfo_t sBootInfo;
|
2015-02-06 14:35:48 +00:00
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
DEFINE PUBLIC FUNCTIONS
|
|
|
|
******************************************************************************/
|
2015-07-14 20:39:07 +01:00
|
|
|
__attribute__ ((section (".boot")))
|
|
|
|
void updater_pre_init (void) {
|
|
|
|
// create the updater lock
|
|
|
|
ASSERT(OSI_OK == sl_LockObjCreate(&updater_LockObj, "UpdaterLock"));
|
|
|
|
}
|
|
|
|
|
2015-02-06 14:35:48 +00:00
|
|
|
bool updater_check_path (void *path) {
|
2015-07-14 20:39:07 +01:00
|
|
|
sl_LockObjLock (&updater_LockObj, SL_OS_WAIT_FOREVER);
|
2015-02-06 14:35:48 +00:00
|
|
|
if (!strcmp(UPDATER_IMG_PATH, path)) {
|
|
|
|
updater_data.fsize = IMG_SIZE;
|
2015-07-14 20:39:07 +01:00
|
|
|
updater_data.path = IMG_UPDATE1;
|
|
|
|
// the launchxl doesn't have enough flash space for 2 user update images
|
|
|
|
#ifdef WIPY
|
|
|
|
// check which one should be the next active image
|
|
|
|
_i32 fhandle;
|
|
|
|
if (!sl_FsOpen((unsigned char *)IMG_BOOT_INFO, FS_MODE_OPEN_READ, NULL, &fhandle)) {
|
|
|
|
ASSERT (sizeof(sBootInfo_t) == sl_FsRead(fhandle, 0, (unsigned char *)&sBootInfo, sizeof(sBootInfo_t)));
|
|
|
|
sl_FsClose(fhandle, 0, 0, 0);
|
2015-09-11 08:34:03 +01:00
|
|
|
// if we still have an image pending for verification, keep overwriting it
|
2015-07-14 20:39:07 +01:00
|
|
|
if ((sBootInfo.Status == IMG_STATUS_CHECK && sBootInfo.ActiveImg == IMG_ACT_UPDATE2) ||
|
2015-09-11 08:34:03 +01:00
|
|
|
(sBootInfo.ActiveImg == IMG_ACT_UPDATE1 && sBootInfo.Status != IMG_STATUS_CHECK)) {
|
2015-07-14 20:39:07 +01:00
|
|
|
updater_data.path = IMG_UPDATE2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2015-06-30 21:11:15 +01:00
|
|
|
} else if (!strcmp(UPDATER_SRVPACK_PATH, path)) {
|
2015-02-06 14:35:48 +00:00
|
|
|
updater_data.path = IMG_SRVPACK;
|
|
|
|
updater_data.fsize = SRVPACK_SIZE;
|
2015-06-30 21:11:15 +01:00
|
|
|
} else if (!strcmp(UPDATER_SIGN_PATH, path)) {
|
2015-02-06 14:35:48 +00:00
|
|
|
updater_data.path = SRVPACK_SIGN;
|
|
|
|
updater_data.fsize = SIGN_SIZE;
|
2015-06-30 21:11:15 +01:00
|
|
|
} else if (!strcmp(UPDATER_CA_PATH, path)) {
|
|
|
|
updater_data.path = CA_FILE;
|
|
|
|
updater_data.fsize = CA_KEY_SIZE;
|
|
|
|
} else if (!strcmp(UPDATER_CERT_PATH, path)) {
|
|
|
|
updater_data.path = CERT_FILE;
|
|
|
|
updater_data.fsize = CA_KEY_SIZE;
|
|
|
|
} else if (!strcmp(UPDATER_KEY_PATH, path)) {
|
|
|
|
updater_data.path = KEY_FILE;
|
|
|
|
updater_data.fsize = CA_KEY_SIZE;
|
|
|
|
} else {
|
2015-07-14 20:39:07 +01:00
|
|
|
sl_LockObjUnlock (&updater_LockObj);
|
2015-06-30 21:11:15 +01:00
|
|
|
return false;
|
2015-02-06 14:35:48 +00:00
|
|
|
}
|
2015-06-30 21:11:15 +01:00
|
|
|
return true;
|
2015-02-06 14:35:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool updater_start (void) {
|
|
|
|
_u32 AccessModeAndMaxSize = FS_MODE_OPEN_WRITE;
|
|
|
|
SlFsFileInfo_t FsFileInfo;
|
2015-06-30 21:11:15 +01:00
|
|
|
bool result = false;
|
|
|
|
|
2015-02-22 16:31:26 +00:00
|
|
|
sl_LockObjLock (&wlan_LockObj, SL_OS_WAIT_FOREVER);
|
2015-02-06 14:35:48 +00:00
|
|
|
if (0 != sl_FsGetInfo((_u8 *)updater_data.path, 0, &FsFileInfo)) {
|
|
|
|
// file doesn't exist, create it
|
|
|
|
AccessModeAndMaxSize = FS_MODE_OPEN_CREATE(updater_data.fsize, 0);
|
|
|
|
}
|
|
|
|
if (!sl_FsOpen((_u8 *)updater_data.path, AccessModeAndMaxSize, NULL, &updater_data.fhandle)) {
|
|
|
|
updater_data.foffset = 0;
|
2015-06-30 21:11:15 +01:00
|
|
|
result = true;
|
2015-02-06 14:35:48 +00:00
|
|
|
}
|
2015-02-22 16:31:26 +00:00
|
|
|
sl_LockObjUnlock (&wlan_LockObj);
|
2015-06-30 21:11:15 +01:00
|
|
|
return result;
|
2015-02-06 14:35:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool updater_write (uint8_t *buf, uint32_t len) {
|
2015-06-30 21:11:15 +01:00
|
|
|
bool result = false;
|
|
|
|
|
|
|
|
sl_LockObjLock (&wlan_LockObj, SL_OS_WAIT_FOREVER);
|
2015-02-06 14:35:48 +00:00
|
|
|
if (len == sl_FsWrite(updater_data.fhandle, updater_data.foffset, buf, len)) {
|
|
|
|
updater_data.foffset += len;
|
2015-06-30 21:11:15 +01:00
|
|
|
result = true;
|
2015-02-06 14:35:48 +00:00
|
|
|
}
|
2015-06-30 21:11:15 +01:00
|
|
|
sl_LockObjUnlock (&wlan_LockObj);
|
|
|
|
|
|
|
|
return result;
|
2015-02-06 14:35:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void updater_finnish (void) {
|
|
|
|
_i32 fhandle;
|
|
|
|
|
|
|
|
if (updater_data.fhandle > 0) {
|
2015-06-30 21:11:15 +01:00
|
|
|
sl_LockObjLock (&wlan_LockObj, SL_OS_WAIT_FOREVER);
|
2015-02-06 14:35:48 +00:00
|
|
|
// close the file being updated
|
|
|
|
sl_FsClose(updater_data.fhandle, NULL, NULL, 0);
|
2015-07-14 20:39:07 +01:00
|
|
|
#ifdef WIPY
|
|
|
|
// if we still have an image pending for verification, leave the boot info as it is
|
|
|
|
if (!strncmp(IMG_PREFIX, updater_data.path, strlen(IMG_PREFIX)) && sBootInfo.Status != IMG_STATUS_CHECK) {
|
|
|
|
#else
|
|
|
|
if (!strncmp(IMG_PREFIX, updater_data.path, strlen(IMG_PREFIX))) {
|
|
|
|
#endif
|
|
|
|
#ifdef DEBUG
|
2015-02-06 14:35:48 +00:00
|
|
|
if (!sl_FsOpen((unsigned char *)IMG_BOOT_INFO, FS_MODE_OPEN_READ, NULL, &fhandle)) {
|
2015-07-14 20:39:07 +01:00
|
|
|
|
2015-02-06 14:35:48 +00:00
|
|
|
ASSERT (sizeof(sBootInfo_t) == sl_FsRead(fhandle, 0, (unsigned char *)&sBootInfo, sizeof(sBootInfo_t)));
|
|
|
|
sl_FsClose(fhandle, 0, 0, 0);
|
2015-07-14 20:39:07 +01:00
|
|
|
#endif
|
|
|
|
// open the boot info file for writing
|
2015-02-06 14:35:48 +00:00
|
|
|
ASSERT (sl_FsOpen((unsigned char *)IMG_BOOT_INFO, FS_MODE_OPEN_WRITE, NULL, &fhandle) == 0);
|
2015-07-14 20:39:07 +01:00
|
|
|
#ifdef DEBUG
|
2015-02-06 14:35:48 +00:00
|
|
|
}
|
|
|
|
else {
|
2015-07-14 20:39:07 +01:00
|
|
|
// the boot info file doesn't exist yet
|
2015-02-06 14:35:48 +00:00
|
|
|
_u32 BootInfoCreateFlag = _FS_FILE_OPEN_FLAG_COMMIT | _FS_FILE_PUBLIC_WRITE | _FS_FILE_PUBLIC_READ;
|
|
|
|
ASSERT (sl_FsOpen ((unsigned char *)IMG_BOOT_INFO, FS_MODE_OPEN_CREATE((2 * sizeof(sBootInfo_t)),
|
|
|
|
BootInfoCreateFlag), NULL, &fhandle) == 0);
|
|
|
|
}
|
2015-07-14 20:39:07 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
// save the new boot info
|
|
|
|
#ifdef WIPY
|
|
|
|
sBootInfo.PrevImg = sBootInfo.ActiveImg;
|
|
|
|
if (sBootInfo.ActiveImg == IMG_ACT_UPDATE1) {
|
|
|
|
sBootInfo.ActiveImg = IMG_ACT_UPDATE2;
|
|
|
|
} else {
|
|
|
|
sBootInfo.ActiveImg = IMG_ACT_UPDATE1;
|
|
|
|
}
|
|
|
|
// the launchxl doesn't have enough flash space for 2 user updates
|
|
|
|
#else
|
|
|
|
sBootInfo.PrevImg = IMG_ACT_FACTORY;
|
|
|
|
sBootInfo.ActiveImg = IMG_ACT_UPDATE1;
|
|
|
|
#endif
|
2015-02-06 14:35:48 +00:00
|
|
|
sBootInfo.Status = IMG_STATUS_CHECK;
|
|
|
|
ASSERT (sizeof(sBootInfo_t) == sl_FsWrite(fhandle, 0, (unsigned char *)&sBootInfo, sizeof(sBootInfo_t)));
|
|
|
|
sl_FsClose(fhandle, 0, 0, 0);
|
|
|
|
}
|
2015-06-30 21:11:15 +01:00
|
|
|
sl_LockObjUnlock (&wlan_LockObj);
|
2015-07-14 20:39:07 +01:00
|
|
|
updater_data.fhandle = -1;
|
2015-02-06 14:35:48 +00:00
|
|
|
}
|
2015-07-14 20:39:07 +01:00
|
|
|
sl_LockObjUnlock (&updater_LockObj);
|
2015-02-06 14:35:48 +00:00
|
|
|
}
|
2015-07-14 20:39:07 +01:00
|
|
|
|