Skip to content

Commit e413350

Browse files
authored
Merge pull request #5531 from tung7970/feature-fota
RTL8195AM - refactor bootloader and fota support
2 parents a6df4ca + a4575a9 commit e413350

File tree

6 files changed

+250
-142
lines changed

6 files changed

+250
-142
lines changed

targets/TARGET_Realtek/TARGET_AMEBA/TARGET_RTL8195A/device/rtl8195a.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,7 @@
4343

4444
#include "rtl8195a_compiler.h"
4545
#include "rtl8195a_platform.h"
46-
47-
46+
#include "rtl8195a_crypto.h"
4847

4948
#define REG32(reg) (*(volatile uint32_t *)(reg))
5049
#define REG16(reg) (*(volatile uint16_t *)(reg))
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/* mbed Microcontroller Library
2+
* Copyright (c) 2013-2017 Realtek Semiconductor Corp.
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
#ifndef MBED_RTL8195A_CRYPTO_H
20+
#define MBED_RTL8195A_CRYPTO_H
21+
22+
extern _LONG_CALL_ uint32_t crc32_get(uint8_t *buf, int len);
23+
24+
#endif

targets/TARGET_Realtek/TARGET_AMEBA/ota_api.c

Lines changed: 82 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
#include <stdio.h>
1716
#include <string.h>
1817

1918
#include "mbed_wait_api.h"
@@ -24,61 +23,105 @@
2423

2524
static flash_t flash_obj;
2625

27-
void OTA_GetImageInfo(imginfo_t *info)
26+
void OTA_ReadHeader(uint32_t base, imginfo_t *img)
2827
{
29-
uint32_t ver_hi, ver_lo;
28+
uint32_t epoch_hi, epoch_lo;
3029

31-
flash_ext_read_word(&flash_obj, info->base + TAG_OFS, &info->tag);
32-
flash_ext_read_word(&flash_obj, info->base + VER_OFS, &ver_lo);
33-
flash_ext_read_word(&flash_obj, info->base + VER_OFS + 4, &ver_hi);
30+
if (base != OTA_REGION1_BASE || base != OTA_REGION2_BASE) {
31+
return;
32+
}
33+
34+
flash_ext_read_word(&flash_obj, base + OTA_TAG_OFS, &img->tag);
35+
flash_ext_read_word(&flash_obj, base + OTA_VER_OFS, &img->ver);
36+
flash_ext_read_word(&flash_obj, base + OTA_EPOCH_OFS, &epoch_hi);
37+
flash_ext_read_word(&flash_obj, base + OTA_EPOCH_OFS + 4, &epoch_lo);
38+
img->timestamp = ((uint64_t)epoch_hi << 32) | (uint64_t) epoch_lo;
3439

35-
if (info->tag == TAG_DOWNLOAD) {
36-
info->ver = ((uint64_t)ver_hi << 32) | (uint64_t) ver_lo;
37-
} else {
38-
info->ver = 0;
40+
flash_ext_read_word(&flash_obj, base + OTA_SIZE_OFS, &img->size);
41+
flash_ext_stream_read(&flash_obj, base + OTA_HASH_OFS, 32, img->hash);
42+
flash_ext_stream_read(&flash_obj, base + OTA_CAMPAIGN_OFS, 16, img->campaign);
43+
flash_ext_read_word(&flash_obj, base + OTA_CRC32_OFS, &img->crc32);
44+
}
45+
46+
bool OTA_CheckHeader(imginfo_t *img)
47+
{
48+
uint8_t *msg;
49+
uint32_t crc;
50+
51+
msg = (uint8_t *)img;
52+
crc = crc32_get(msg, OTA_CRC32_LEN);
53+
if (crc != img->crc32) {
54+
return false;
55+
}
56+
57+
if ((img->tag & OTA_TAG_CHIP_MSK) != (OTA_TAG_ID & OTA_TAG_CHIP_MSK)) {
58+
return false;
3959
}
60+
61+
return true;
4062
}
4163

42-
uint32_t OTA_GetBase(void)
64+
void OTA_GetImageInfo(uint32_t base, imginfo_t *img)
4365
{
44-
static uint32_t ota_base = 0;
45-
imginfo_t region1, region2;
66+
OTA_ReadHeader(base, img);
4667

47-
if (ota_base == OTA_REGION1 || ota_base == OTA_REGION2) {
48-
return ota_base;
68+
if (!OTA_CheckHeader(img)) {
69+
img->timestamp = 0;
70+
img->valid = false;
4971
}
5072

51-
region1.base = OTA_REGION1;
52-
region2.base = OTA_REGION2;
73+
img->valid = true;
74+
}
75+
76+
uint32_t OTA_GetUpdateBase(void)
77+
{
78+
imginfo_t img1, img2;
79+
80+
OTA_GetImageInfo(OTA_REGION1_BASE, &img1);
81+
OTA_GetImageInfo(OTA_REGION2_BASE, &img2);
5382

54-
OTA_GetImageInfo(&region1);
55-
OTA_GetImageInfo(&region2);
83+
if (img1.valid && img2.valid) {
84+
if (img1.timestamp < img2.timestamp) {
85+
return OTA_REGION1_BASE;
86+
} else {
87+
return OTA_REGION2_BASE;
88+
}
89+
}
5690

57-
if (region1.ver >= region2.ver) {
58-
ota_base = region2.base;
59-
} else {
60-
ota_base = region1.base;
91+
if (img1.valid) {
92+
return OTA_REGION2_BASE;
6193
}
62-
return ota_base;
94+
95+
return OTA_REGION1_BASE;
6396
}
6497

65-
uint32_t OTA_MarkUpdateDone(void)
98+
uint32_t OTA_UpateHeader(uint32_t base, imginfo_t *img)
6699
{
67-
uint32_t addr = OTA_GetBase() + TAG_OFS;
100+
flash_ext_write_word(&flash_obj, base + OTA_TAG_OFS, img->tag);
101+
flash_ext_write_word(&flash_obj, base + OTA_VER_OFS, img->ver);
102+
flash_ext_write_word(&flash_obj, base + OTA_EPOCH_OFS, img->timestamp >> 32);
103+
flash_ext_write_word(&flash_obj, base + OTA_EPOCH_OFS + 4, (img->timestamp << 32) >> 32);
68104

69-
return flash_ext_write_word(&flash_obj, addr, TAG_DOWNLOAD);
105+
flash_ext_write_word(&flash_obj, base + OTA_SIZE_OFS, img->size);
106+
flash_ext_stream_write(&flash_obj, base + OTA_HASH_OFS, 32, img->hash);
107+
flash_ext_stream_write(&flash_obj, base + OTA_CAMPAIGN_OFS, 16, img->campaign);
108+
flash_ext_write_word(&flash_obj, base + OTA_CRC32_OFS, img->crc32);
109+
110+
return 0;
70111
}
71112

72-
uint32_t OTA_UpdateImage(uint32_t offset, uint32_t len, uint8_t *data)
113+
uint32_t OTA_UpdateImage(uint32_t base, uint32_t offset, uint32_t len, uint8_t *data)
73114
{
74115
uint32_t addr, start, end, count, shift;
75116
uint8_t *pdata = data;
76117
uint8_t buf[FLASH_SECTOR_SIZE];
77118

78-
start = OTA_GetBase() + offset;
119+
start = base + offset;
79120
end = start + len;
80121

81-
if (data == NULL || start > FLASH_TOP || end > FLASH_TOP) {
122+
if (data == NULL ||
123+
base != OTA_REGION1_BASE || base != OTA_REGION2_BASE ||
124+
start > FLASH_TOP || end > FLASH_TOP) {
82125
return 0;
83126
}
84127

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

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

109-
uint32_t OTA_ReadImage(uint32_t offset, uint32_t len, uint8_t *data)
151+
uint32_t OTA_ReadImage(uint32_t base, uint32_t offset, uint32_t len, uint8_t *data)
110152
{
111-
uint32_t addr, endaddr;
153+
uint32_t start, end;
112154

113-
addr = OTA_GetBase() + offset;
114-
endaddr = addr + len;
155+
start = base + offset;
156+
end = start + len;
115157

116-
if (data == NULL || addr > FLASH_TOP || endaddr > FLASH_TOP) {
158+
if (data == NULL ||
159+
base != OTA_REGION1_BASE || base != OTA_REGION2_BASE ||
160+
start > FLASH_TOP || end > FLASH_TOP) {
117161
return 0;
118162
}
119163

120-
printf("OTA: read addr=0x%lx\r\n", addr);
121-
return flash_ext_stream_read(&flash_obj, addr, len, data);
164+
return flash_ext_stream_read(&flash_obj, start, len, data);
122165
}
123166

124167
void OTA_ResetTarget(void)
125168
{
126169
__RTK_CTRL_WRITE32(0x14, 0x00000021);
127170
wait(1);
128171

129-
// write SCB->AIRCR
130-
HAL_WRITE32(0xE000ED00, 0x0C,
131-
(0x5FA << 16) | // VECTKEY
132-
(HAL_READ32(0xE000ED00, 0x0C) & (7 << 8)) | // PRIGROUP
133-
(1 << 2)); // SYSRESETREQ
172+
NVIC_SystemReset();
134173

135174
// not reached
136175
while (1);

targets/TARGET_Realtek/TARGET_AMEBA/ota_api.h

Lines changed: 58 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,79 @@
1+
/* mbed Microcontroller Library
2+
* Copyright (c) 2013-2017 Realtek Semiconductor Corp.
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
118
#ifndef MBED_OTA_API_H
219
#define MBED_OTA_API_H
320

4-
#define FLASH_TOP 0x200000
5-
#define FLASH_SECTOR_SIZE 0x1000
6-
#define FLASH_SECTOR_MASK ~(FLASH_SECTOR_SIZE - 1)
7-
#define OTA_REGION1 0x0b000
8-
#define OTA_REGION2 0xc0000
9-
#define TAG_OFS 0xc
10-
#define VER_OFS 0x10
21+
#define FLASH_TOP 0x200000
22+
#define FLASH_SECTOR_SIZE 0x1000
23+
#define FLASH_SECTOR_MASK ~(FLASH_SECTOR_SIZE - 1)
1124

12-
#define TAG_DOWNLOAD 0x81950001
13-
#define TAG_VERIFIED 0x81950003
25+
#define OTA_REGION1_BASE 0x40000
26+
#define OTA_REGION2_BASE 0x120000
27+
#define OTA_REGION1_SIZE 0xe0000
28+
#define OTA_REGION2_SIZE 0xe0000
29+
#define OTA_REGION_SIZE 0xe0000
30+
#define OTA_MBED_FS_BASE 0xb000
31+
32+
#define OTA_CRC32_LEN 0x44
33+
#define OTA_HEADER_LEN 0x48
34+
35+
#define OTA_HEADER_OFS 0x0
36+
#define OTA_TAG_OFS 0x0
37+
#define OTA_VER_OFS 0x4
38+
#define OTA_EPOCH_OFS 0x8
39+
#define OTA_SIZE_OFS 0x10
40+
#define OTA_HASH_OFS 0x14
41+
#define OTA_CAMPAIGN_OFS 0x34
42+
#define OTA_CRC32_OFS 0x44
43+
#define OTA_IMAGE_OFS 0x48
44+
45+
#define OTA_TAG_ID 0x81950001
46+
#define OTA_VER_ID 0x81950001
47+
48+
#define OTA_TAG_CHIP_MSK 0xFFFF0000
49+
#define OTA_TAG_INFO_MSK 0x0000FFFF
1450

1551
typedef struct imginfo_s {
16-
uint32_t base;
1752
uint32_t tag;
18-
uint64_t ver;
53+
uint32_t ver;
54+
uint64_t timestamp;
55+
uint32_t size;
56+
uint8_t hash[32];
57+
uint8_t campaign[16];
58+
uint32_t crc32;
59+
bool valid;
1960
} imginfo_t;
2061

2162
#ifdef __cplusplus
2263
extern "C" {
2364
#endif
2465

25-
extern void OTA_GetImageInfo(imginfo_t *info);
26-
extern uint32_t OTA_GetBase(void);
66+
extern void OTA_GetImageInfo(uint32_t base, imginfo_t *info);
67+
extern uint32_t OTA_GetUpdateBase(void);
2768

28-
extern uint32_t OTA_UpdateImage(uint32_t offset, uint32_t len, uint8_t *data);
29-
extern uint32_t OTA_ReadImage(uint32_t offset, uint32_t len, uint8_t *data);
30-
extern uint32_t OTA_MarkUpdateDone(void);
69+
extern uint32_t OTA_UpdateHeader(uint32_t base, imginfo_t *img);
70+
extern uint32_t OTA_UpdateImage(uint32_t base, uint32_t offset, uint32_t len, uint8_t *data);
71+
extern void OTA_ReadHeader(uint32_t base, imginfo_t *img);
72+
extern uint32_t OTA_ReadImage(uint32_t base, uint32_t offset, uint32_t len, uint8_t *data);
3173
extern void OTA_ResetTarget(void);
3274

3375
#ifdef __cplusplus
3476
}
3577
#endif
3678

3779
#endif /* MBED_OTA_API_H */
38-
212 KB
Binary file not shown.

0 commit comments

Comments
 (0)