Skip to content

RTL8195AM - refactor bootloader and fota support #5531

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 30, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@

#include "rtl8195a_compiler.h"
#include "rtl8195a_platform.h"


#include "rtl8195a_crypto.h"

#define REG32(reg) (*(volatile uint32_t *)(reg))
#define REG16(reg) (*(volatile uint16_t *)(reg))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/* mbed Microcontroller Library
* Copyright (c) 2013-2017 Realtek Semiconductor Corp.
*
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#ifndef MBED_RTL8195A_CRYPTO_H
#define MBED_RTL8195A_CRYPTO_H

extern _LONG_CALL_ uint32_t crc32_get(uint8_t *buf, int len);

#endif
125 changes: 82 additions & 43 deletions targets/TARGET_Realtek/TARGET_AMEBA/ota_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <stdio.h>
#include <string.h>

#include "mbed_wait_api.h"
Expand All @@ -24,61 +23,105 @@

static flash_t flash_obj;

void OTA_GetImageInfo(imginfo_t *info)
void OTA_ReadHeader(uint32_t base, imginfo_t *img)
{
uint32_t ver_hi, ver_lo;
uint32_t epoch_hi, epoch_lo;

flash_ext_read_word(&flash_obj, info->base + TAG_OFS, &info->tag);
flash_ext_read_word(&flash_obj, info->base + VER_OFS, &ver_lo);
flash_ext_read_word(&flash_obj, info->base + VER_OFS + 4, &ver_hi);
if (base != OTA_REGION1_BASE || base != OTA_REGION2_BASE) {
return;
}

flash_ext_read_word(&flash_obj, base + OTA_TAG_OFS, &img->tag);
flash_ext_read_word(&flash_obj, base + OTA_VER_OFS, &img->ver);
flash_ext_read_word(&flash_obj, base + OTA_EPOCH_OFS, &epoch_hi);
flash_ext_read_word(&flash_obj, base + OTA_EPOCH_OFS + 4, &epoch_lo);
img->timestamp = ((uint64_t)epoch_hi << 32) | (uint64_t) epoch_lo;

if (info->tag == TAG_DOWNLOAD) {
info->ver = ((uint64_t)ver_hi << 32) | (uint64_t) ver_lo;
} else {
info->ver = 0;
flash_ext_read_word(&flash_obj, base + OTA_SIZE_OFS, &img->size);
flash_ext_stream_read(&flash_obj, base + OTA_HASH_OFS, 32, img->hash);
flash_ext_stream_read(&flash_obj, base + OTA_CAMPAIGN_OFS, 16, img->campaign);
flash_ext_read_word(&flash_obj, base + OTA_CRC32_OFS, &img->crc32);
}

bool OTA_CheckHeader(imginfo_t *img)
{
uint8_t *msg;
uint32_t crc;

msg = (uint8_t *)img;
crc = crc32_get(msg, OTA_CRC32_LEN);
if (crc != img->crc32) {
return false;
}

if ((img->tag & OTA_TAG_CHIP_MSK) != (OTA_TAG_ID & OTA_TAG_CHIP_MSK)) {
return false;
}

return true;
}

uint32_t OTA_GetBase(void)
void OTA_GetImageInfo(uint32_t base, imginfo_t *img)
{
static uint32_t ota_base = 0;
imginfo_t region1, region2;
OTA_ReadHeader(base, img);

if (ota_base == OTA_REGION1 || ota_base == OTA_REGION2) {
return ota_base;
if (!OTA_CheckHeader(img)) {
img->timestamp = 0;
img->valid = false;
}

region1.base = OTA_REGION1;
region2.base = OTA_REGION2;
img->valid = true;
}

uint32_t OTA_GetUpdateBase(void)
{
imginfo_t img1, img2;

OTA_GetImageInfo(OTA_REGION1_BASE, &img1);
OTA_GetImageInfo(OTA_REGION2_BASE, &img2);

OTA_GetImageInfo(&region1);
OTA_GetImageInfo(&region2);
if (img1.valid && img2.valid) {
if (img1.timestamp < img2.timestamp) {
return OTA_REGION1_BASE;
} else {
return OTA_REGION2_BASE;
}
}

if (region1.ver >= region2.ver) {
ota_base = region2.base;
} else {
ota_base = region1.base;
if (img1.valid) {
return OTA_REGION2_BASE;
}
return ota_base;

return OTA_REGION1_BASE;
}

uint32_t OTA_MarkUpdateDone(void)
uint32_t OTA_UpateHeader(uint32_t base, imginfo_t *img)
{
uint32_t addr = OTA_GetBase() + TAG_OFS;
flash_ext_write_word(&flash_obj, base + OTA_TAG_OFS, img->tag);
flash_ext_write_word(&flash_obj, base + OTA_VER_OFS, img->ver);
flash_ext_write_word(&flash_obj, base + OTA_EPOCH_OFS, img->timestamp >> 32);
flash_ext_write_word(&flash_obj, base + OTA_EPOCH_OFS + 4, (img->timestamp << 32) >> 32);

return flash_ext_write_word(&flash_obj, addr, TAG_DOWNLOAD);
flash_ext_write_word(&flash_obj, base + OTA_SIZE_OFS, img->size);
flash_ext_stream_write(&flash_obj, base + OTA_HASH_OFS, 32, img->hash);
flash_ext_stream_write(&flash_obj, base + OTA_CAMPAIGN_OFS, 16, img->campaign);
flash_ext_write_word(&flash_obj, base + OTA_CRC32_OFS, img->crc32);

return 0;
}

uint32_t OTA_UpdateImage(uint32_t offset, uint32_t len, uint8_t *data)
uint32_t OTA_UpdateImage(uint32_t base, uint32_t offset, uint32_t len, uint8_t *data)
{
uint32_t addr, start, end, count, shift;
uint8_t *pdata = data;
uint8_t buf[FLASH_SECTOR_SIZE];

start = OTA_GetBase() + offset;
start = base + offset;
end = start + len;

if (data == NULL || start > FLASH_TOP || end > FLASH_TOP) {
if (data == NULL ||
base != OTA_REGION1_BASE || base != OTA_REGION2_BASE ||
start > FLASH_TOP || end > FLASH_TOP) {
return 0;
}

Expand All @@ -96,7 +139,6 @@ uint32_t OTA_UpdateImage(uint32_t offset, uint32_t len, uint8_t *data)
}

while (addr < end) {
printf("OTA: update addr=0x%lx, len=%ld\r\n", addr, len);
count = MIN(FLASH_SECTOR_SIZE, end - addr);
flash_ext_erase_sector(&flash_obj, addr);
flash_ext_stream_write(&flash_obj, addr, count, pdata);
Expand All @@ -106,31 +148,28 @@ uint32_t OTA_UpdateImage(uint32_t offset, uint32_t len, uint8_t *data)
return len;
}

uint32_t OTA_ReadImage(uint32_t offset, uint32_t len, uint8_t *data)
uint32_t OTA_ReadImage(uint32_t base, uint32_t offset, uint32_t len, uint8_t *data)
{
uint32_t addr, endaddr;
uint32_t start, end;

addr = OTA_GetBase() + offset;
endaddr = addr + len;
start = base + offset;
end = start + len;

if (data == NULL || addr > FLASH_TOP || endaddr > FLASH_TOP) {
if (data == NULL ||
base != OTA_REGION1_BASE || base != OTA_REGION2_BASE ||
start > FLASH_TOP || end > FLASH_TOP) {
return 0;
}

printf("OTA: read addr=0x%lx\r\n", addr);
return flash_ext_stream_read(&flash_obj, addr, len, data);
return flash_ext_stream_read(&flash_obj, start, len, data);
}

void OTA_ResetTarget(void)
{
__RTK_CTRL_WRITE32(0x14, 0x00000021);
wait(1);

// write SCB->AIRCR
HAL_WRITE32(0xE000ED00, 0x0C,
(0x5FA << 16) | // VECTKEY
(HAL_READ32(0xE000ED00, 0x0C) & (7 << 8)) | // PRIGROUP
(1 << 2)); // SYSRESETREQ
NVIC_SystemReset();

// not reached
while (1);
Expand Down
75 changes: 58 additions & 17 deletions targets/TARGET_Realtek/TARGET_AMEBA/ota_api.h
Original file line number Diff line number Diff line change
@@ -1,38 +1,79 @@
/* mbed Microcontroller Library
* Copyright (c) 2013-2017 Realtek Semiconductor Corp.
*
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef MBED_OTA_API_H
#define MBED_OTA_API_H

#define FLASH_TOP 0x200000
#define FLASH_SECTOR_SIZE 0x1000
#define FLASH_SECTOR_MASK ~(FLASH_SECTOR_SIZE - 1)
#define OTA_REGION1 0x0b000
#define OTA_REGION2 0xc0000
#define TAG_OFS 0xc
#define VER_OFS 0x10
#define FLASH_TOP 0x200000
#define FLASH_SECTOR_SIZE 0x1000
#define FLASH_SECTOR_MASK ~(FLASH_SECTOR_SIZE - 1)

#define TAG_DOWNLOAD 0x81950001
#define TAG_VERIFIED 0x81950003
#define OTA_REGION1_BASE 0x40000
#define OTA_REGION2_BASE 0x120000
#define OTA_REGION1_SIZE 0xe0000
#define OTA_REGION2_SIZE 0xe0000
#define OTA_REGION_SIZE 0xe0000
#define OTA_MBED_FS_BASE 0xb000

#define OTA_CRC32_LEN 0x44
#define OTA_HEADER_LEN 0x48

#define OTA_HEADER_OFS 0x0
#define OTA_TAG_OFS 0x0
#define OTA_VER_OFS 0x4
#define OTA_EPOCH_OFS 0x8
#define OTA_SIZE_OFS 0x10
#define OTA_HASH_OFS 0x14
#define OTA_CAMPAIGN_OFS 0x34
#define OTA_CRC32_OFS 0x44
#define OTA_IMAGE_OFS 0x48

#define OTA_TAG_ID 0x81950001
#define OTA_VER_ID 0x81950001

#define OTA_TAG_CHIP_MSK 0xFFFF0000
#define OTA_TAG_INFO_MSK 0x0000FFFF

typedef struct imginfo_s {
uint32_t base;
uint32_t tag;
uint64_t ver;
uint32_t ver;
uint64_t timestamp;
uint32_t size;
uint8_t hash[32];
uint8_t campaign[16];
uint32_t crc32;
bool valid;
} imginfo_t;

#ifdef __cplusplus
extern "C" {
#endif

extern void OTA_GetImageInfo(imginfo_t *info);
extern uint32_t OTA_GetBase(void);
extern void OTA_GetImageInfo(uint32_t base, imginfo_t *info);
extern uint32_t OTA_GetUpdateBase(void);

extern uint32_t OTA_UpdateImage(uint32_t offset, uint32_t len, uint8_t *data);
extern uint32_t OTA_ReadImage(uint32_t offset, uint32_t len, uint8_t *data);
extern uint32_t OTA_MarkUpdateDone(void);
extern uint32_t OTA_UpdateHeader(uint32_t base, imginfo_t *img);
extern uint32_t OTA_UpdateImage(uint32_t base, uint32_t offset, uint32_t len, uint8_t *data);
extern void OTA_ReadHeader(uint32_t base, imginfo_t *img);
extern uint32_t OTA_ReadImage(uint32_t base, uint32_t offset, uint32_t len, uint8_t *data);
extern void OTA_ResetTarget(void);

#ifdef __cplusplus
}
#endif

#endif /* MBED_OTA_API_H */

Binary file modified tools/bootloaders/REALTEK_RTL8195AM/ram_1.bin
Binary file not shown.
Loading