Skip to content

Commit 65e9cac

Browse files
committed
Updated the code fix of timer and Mbed TLS.
1 parent a26e0d1 commit 65e9cac

File tree

7 files changed

+76
-48
lines changed

7 files changed

+76
-48
lines changed

components/storage/blockdevice/COMPONENT_FLASHIAP/mbed_lib.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@
1818
"FVP_MPS2": {
1919
"base-address": "0x00200000",
2020
"size": "0x200000"
21+
},
22+
"S5JS100": {
23+
"base-address": "0x40EF5000",
24+
"size": "0x80000"
2125
}
2226
}
2327
}

features/mbedtls/targets/TARGET_Samsung/sha/sha256_alt.c

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -140,10 +140,10 @@ int mbedtls_sha256_update_ret(mbedtls_sha256_context *ctx, const unsigned char *
140140
if (ctx->is224) {
141141
mbedtls_sha256_sw_update_ret(ctx, input, ilen);
142142
} else {
143-
if (ilen > MAX_MB_HASH_BLOCK_BLEN || ctx->totals > MAX_MB_HASH_BLOCK_BLEN) {
143+
if (ilen > MAX_MB_HASH_BLOCK_BLEN || (ctx->totals + ilen) > MAX_MB_HASH_BLOCK_BLEN || ctx->totals > MAX_MB_HASH_BLOCK_BLEN) {
144144
// H/W SHA has limitation to seperated API with oversized message.
145145
// fall back to S/W SHA-256
146-
if (ctx->totals == 0) {
146+
if (ctx->totals == 0 || ctx->hw == 1) {
147147
ctx->total[0] = 0;
148148
ctx->total[1] = 0;
149149
/* SHA-256 */
@@ -157,11 +157,17 @@ int mbedtls_sha256_update_ret(mbedtls_sha256_context *ctx, const unsigned char *
157157
ctx->state[7] = 0x5BE0CD19;
158158
}
159159
ctx->totals += ilen;
160+
//in case, H/W -> S/W fallback case
161+
if ((ctx->totals + ilen) > MAX_MB_HASH_BLOCK_BLEN && ctx->hw == 1) {
162+
mbedtls_sha512_sw_update_ret(ctx, ctx->sbuf, ctx->pstMessage.u32DataByteLen);
163+
}
164+
ctx->hw = 0;
160165
mbedtls_sha256_sw_update_ret(ctx, input, ilen);
161-
} else {
166+
} else { //less than MAX_MB_HASH_BLOCK_BLEN size will handle with H/W
162167
// SHA-256 handle by SSS H/W
163168
memcpy(ctx->sbuf + ctx->pstMessage.u32DataByteLen, input, ilen);
164169
ctx->pstMessage.u32DataByteLen += ilen;
170+
ctx->totals += ilen; //in case the block size increased incrementally. (3, 20, 256..)
165171
}
166172
}
167173

features/mbedtls/targets/TARGET_Samsung/sha/sha256_alt.h

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ typedef struct mbedtls_sha256_context_s {
6161

6262
/* for H/W SHA-256 */
6363
uint32_t totals;
64+
uint32_t hw;
6465
unsigned char sbuf[ST_SHA256_BUF_SIZE]; /*!< ST_SHA256_BLOCK_SIZE buffer to store values so that algorithm is called once the buffer is filled */
6566
stOCTET_STRING pstMessage;
6667
stOCTET_STRING pstDigest;
@@ -143,7 +144,7 @@ int mbedtls_internal_sha256_process(mbedtls_sha256_context *ctx, const unsigned
143144
* <li>1: Use SHA-224.</li></ul>
144145
*/
145146
MBEDTLS_DEPRECATED void mbedtls_sha256_starts(mbedtls_sha256_context *ctx,
146-
int is224);
147+
int is224);
147148

148149
/**
149150
* \brief This function feeds an input buffer into an ongoing
@@ -156,8 +157,8 @@ MBEDTLS_DEPRECATED void mbedtls_sha256_starts(mbedtls_sha256_context *ctx,
156157
* \param ilen The length of the input data.
157158
*/
158159
MBEDTLS_DEPRECATED void mbedtls_sha256_update(mbedtls_sha256_context *ctx,
159-
const unsigned char *input,
160-
size_t ilen);
160+
const unsigned char *input,
161+
size_t ilen);
161162

162163
/**
163164
* \brief This function finishes the SHA-256 operation, and writes
@@ -169,7 +170,7 @@ MBEDTLS_DEPRECATED void mbedtls_sha256_update(mbedtls_sha256_context *ctx,
169170
* \param output The SHA-224or SHA-256 checksum result.
170171
*/
171172
MBEDTLS_DEPRECATED void mbedtls_sha256_finish(mbedtls_sha256_context *ctx,
172-
unsigned char output[32]);
173+
unsigned char output[32]);
173174

174175
/**
175176
* \brief This function processes a single data block within
@@ -182,7 +183,7 @@ MBEDTLS_DEPRECATED void mbedtls_sha256_finish(mbedtls_sha256_context *ctx,
182183
* \param data The buffer holding one block of data.
183184
*/
184185
MBEDTLS_DEPRECATED void mbedtls_sha256_process(mbedtls_sha256_context *ctx,
185-
const unsigned char data[64]);
186+
const unsigned char data[64]);
186187

187188
#undef MBEDTLS_DEPRECATED
188189
#endif /* !MBEDTLS_DEPRECATED_REMOVED */

features/mbedtls/targets/TARGET_Samsung/sha/sha512_alt.c

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -146,10 +146,10 @@ int mbedtls_sha512_starts_ret(mbedtls_sha512_context *ctx, int is384)
146146
*/
147147
int mbedtls_sha512_update_ret(mbedtls_sha512_context *ctx, const unsigned char *input, size_t ilen)
148148
{
149-
if (ilen > MAX_MB_HASH_BLOCK_BLEN || ctx->totals > MAX_MB_HASH_BLOCK_BLEN) {
149+
if (ilen > MAX_MB_HASH_BLOCK_BLEN || (ctx->totals + ilen) > MAX_MB_HASH_BLOCK_BLEN || ctx->totals > MAX_MB_HASH_BLOCK_BLEN) {
150150
// H/W SHA has limitation to seperated API with oversized message.
151-
// fall back to S/W SHA-512
152-
if (ctx->totals == 0) {
151+
// fallback to S/W from H/W pre-tested
152+
if (ctx->totals == 0 || ctx->hw == 1) {
153153
ctx->total[0] = 0;
154154
ctx->total[1] = 0;
155155
if (ctx->is384) {
@@ -175,11 +175,19 @@ int mbedtls_sha512_update_ret(mbedtls_sha512_context *ctx, const unsigned char *
175175
}
176176
}
177177
ctx->totals += ilen;
178+
//in case, H/W -> S/W fallback case
179+
if ((ctx->totals + ilen) > MAX_MB_HASH_BLOCK_BLEN && ctx->hw == 1) {
180+
//214 + 2577
181+
mbedtls_sha512_sw_update_ret(ctx, ctx->sbuf, ctx->pstMessage.u32DataByteLen);
182+
}
183+
ctx->hw = 0;
178184
mbedtls_sha512_sw_update_ret(ctx, input, ilen);
179185
} else {
180186
// SHA-256 handle by SSS H/W
181187
memcpy(ctx->sbuf + ctx->pstMessage.u32DataByteLen, input, ilen);
182188
ctx->pstMessage.u32DataByteLen += ilen;
189+
ctx->totals += ilen;
190+
ctx->hw = 1;
183191
}
184192

185193
return 0;

features/mbedtls/targets/TARGET_Samsung/sha/sha512_alt.h

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ typedef struct mbedtls_sha512_context_s {
5959
0: Use SHA-512, or 1: Use SHA-384. */
6060
/* for H/W SHA-512 */
6161
uint32_t totals;
62+
uint32_t hw;
6263
unsigned char sbuf[ST_SHA512_BUF_SIZE]; /*!< ST_SHA512_BLOCK_SIZE buffer to store values so that algorithm is called once the buffer is filled */
6364
stOCTET_STRING pstMessage;
6465
stOCTET_STRING pstDigest;
@@ -141,7 +142,7 @@ int mbedtls_internal_sha512_process(mbedtls_sha512_context *ctx, const unsigned
141142
* <li>1: Use SHA-224.</li></ul>
142143
*/
143144
MBEDTLS_DEPRECATED void mbedtls_sha512_starts(mbedtls_sha512_context *ctx,
144-
int is224);
145+
int is224);
145146

146147
/**
147148
* \brief This function feeds an input buffer into an ongoing
@@ -154,8 +155,8 @@ MBEDTLS_DEPRECATED void mbedtls_sha512_starts(mbedtls_sha512_context *ctx,
154155
* \param ilen The length of the input data.
155156
*/
156157
MBEDTLS_DEPRECATED void mbedtls_sha512_update(mbedtls_sha512_context *ctx,
157-
const unsigned char *input,
158-
size_t ilen);
158+
const unsigned char *input,
159+
size_t ilen);
159160

160161
/**
161162
* \brief This function finishes the SHA-512 operation, and writes
@@ -167,7 +168,7 @@ MBEDTLS_DEPRECATED void mbedtls_sha512_update(mbedtls_sha512_context *ctx,
167168
* \param output The SHA-224or SHA-512 checksum result.
168169
*/
169170
MBEDTLS_DEPRECATED void mbedtls_sha512_finish(mbedtls_sha512_context *ctx,
170-
unsigned char output[64]);
171+
unsigned char output[64]);
171172

172173
/**
173174
* \brief This function processes a single data block within
@@ -180,7 +181,7 @@ MBEDTLS_DEPRECATED void mbedtls_sha512_finish(mbedtls_sha512_context *ctx,
180181
* \param data The buffer holding one block of data.
181182
*/
182183
MBEDTLS_DEPRECATED void mbedtls_sha512_process(mbedtls_sha512_context *ctx,
183-
const unsigned char data[128]);
184+
const unsigned char data[128]);
184185

185186
#undef MBEDTLS_DEPRECATED
186187
#endif /* !MBEDTLS_DEPRECATED_REMOVED */

targets/TARGET_Samsung/TARGET_SIDK_S5JS100/device/cmsis_nvic_virtual.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,27 @@
1+
/*
2+
* Copyright (c) 2019 Arm Limited
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+
/* @file : cmsis_nvic_virtual.h
19+
* @brief : NVIC functions list
20+
* @date : December 2019
21+
* @note : List of NVIC macro and customize TFM interrupt
22+
*/
23+
24+
125
#include "cmsis.h"
226

327
#ifndef NVIC_VIRTUAL_H

targets/TARGET_Samsung/TARGET_SIDK_S5JS100/us_ticker.c

Lines changed: 16 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -27,29 +27,28 @@
2727

2828
#define TIMER_TARGET_COUNT_DEFAULT 0xFFFFFFFF
2929

30-
volatile uint32_t us_ticker_initialized = 0;
31-
volatile uint32_t us_user_intset;
32-
volatile uint32_t g_us_last_return = 0;
30+
uint32_t us_ticker_initialized = 0;
31+
uint32_t g_us_last_return = 0;
3332

3433
void us_ticker_enable_interrupt(void);
3534
void us_ticker_disable_interrupt(void);
3635

3736
const ticker_info_t *us_ticker_get_info()
3837
{
3938
static const ticker_info_t info = {
40-
1000000, //1Mhz
39+
26000000, //26Mhz
4140
32 //32bit counter
4241
};
4342
return &info;
4443
}
4544

4645
static void enable_timer0(void)
4746
{
48-
putreg32(1, S5JS100_TIMER0_BASE + S5JS100_TIMER_UP_DOWN_SEL); // Set Up count
49-
putreg32(0x0, S5JS100_TIMER0_BASE + S5JS100_TIMER_LOAD_VALUE);
47+
putreg32(0, S5JS100_TIMER0_BASE + S5JS100_TIMER_UP_DOWN_SEL); // Set Up count
48+
putreg32(TIMER_TARGET_COUNT_DEFAULT, S5JS100_TIMER0_BASE + S5JS100_TIMER_LOAD_VALUE);
5049
putreg32(0x1, S5JS100_TIMER0_BASE + S5JS100_TIMER_CDC_ENABLE);
51-
putreg32(0, S5JS100_TIMER0_BASE + S5JS100_TIMER_CDC_COUNT_VALUE);
52-
putreg32(TIMER_TARGET_COUNT_DEFAULT, S5JS100_TIMER0_BASE + S5JS100_TIMER_INT_SEL);
50+
putreg32(TIMER_TARGET_COUNT_DEFAULT, S5JS100_TIMER0_BASE + S5JS100_TIMER_CDC_COUNT_VALUE);
51+
putreg32(0, S5JS100_TIMER0_BASE + S5JS100_TIMER_INT_SEL);
5352
putreg32(0, S5JS100_TIMER0_BASE + S5JS100_TIMER_INT_ENABLE);
5453
putreg32(3, S5JS100_TIMER0_BASE + S5JS100_TIMER_CONTROL);
5554
}
@@ -67,18 +66,18 @@ static void disable_timer0(void)
6766

6867
static void enable_timer1(void)
6968
{
70-
putreg32(1, S5JS100_TIMER1_BASE + S5JS100_TIMER_UP_DOWN_SEL); // Set Up count
71-
putreg32(0x0, S5JS100_TIMER1_BASE + S5JS100_TIMER_LOAD_VALUE);
72-
putreg32(TIMER_TARGET_COUNT_DEFAULT, S5JS100_TIMER1_BASE + S5JS100_TIMER_INT_SEL);
69+
putreg32(0, S5JS100_TIMER1_BASE + S5JS100_TIMER_UP_DOWN_SEL); // Set Up count
70+
putreg32(TIMER_TARGET_COUNT_DEFAULT, S5JS100_TIMER1_BASE + S5JS100_TIMER_LOAD_VALUE);
71+
putreg32(0, S5JS100_TIMER1_BASE + S5JS100_TIMER_INT_SEL);
7372
putreg32(0, S5JS100_TIMER1_BASE + S5JS100_TIMER_INT_ENABLE);
7473
putreg32(3, S5JS100_TIMER1_BASE + S5JS100_TIMER_CONTROL);
7574
}
7675

7776
static void disable_timer1(void)
7877
{
7978
putreg32(0, S5JS100_TIMER1_BASE + S5JS100_TIMER_UP_DOWN_SEL);
80-
putreg32(0xFFFFFFFF, S5JS100_TIMER1_BASE + S5JS100_TIMER_LOAD_VALUE);
81-
putreg32(TIMER_TARGET_COUNT_DEFAULT, S5JS100_TIMER1_BASE + S5JS100_TIMER_INT_SEL);
79+
putreg32(TIMER_TARGET_COUNT_DEFAULT, S5JS100_TIMER1_BASE + S5JS100_TIMER_LOAD_VALUE);
80+
putreg32(0, S5JS100_TIMER1_BASE + S5JS100_TIMER_INT_SEL);
8281
putreg32(0, S5JS100_TIMER1_BASE + S5JS100_TIMER_CONTROL);
8382
putreg32(0, S5JS100_TIMER1_BASE + S5JS100_TIMER_INT_ENABLE);
8483
}
@@ -118,31 +117,16 @@ uint32_t us_ticker_read()
118117
if (!us_ticker_initialized) {
119118
us_ticker_init();
120119
}
121-
volatile uint32_t current_count = getreg32(S5JS100_TIMER0_BASE + S5JS100_TIMER_CDC_COUNT_VALUE) / 26;
120+
uint32_t current_count = TIMER_TARGET_COUNT_DEFAULT - getreg32(S5JS100_TIMER0_BASE + S5JS100_TIMER_CDC_COUNT_VALUE);
122121
g_us_last_return = current_count;
123122
return current_count;
124123
}
125124

126125
void us_ticker_set_interrupt(timestamp_t timestamp)
127126
{
128-
if (timestamp < 0x70000000 && timestamp != 0) {
129-
uint32_t temp = g_us_last_return;
130-
us_user_intset = timestamp - g_us_last_return;
131-
/* keep to check
132-
if (us_user_intset < 0) {
133-
us_ticker_irq_handler();
134-
return;
135-
}*/
136-
us_user_intset = us_user_intset * 26;
137-
uint32_t past_tick = us_ticker_read() - temp;
138-
putreg32(0, S5JS100_TIMER1_BASE + S5JS100_TIMER_LOAD_CON_VALUE);
139-
putreg32(us_user_intset - past_tick * 26, S5JS100_TIMER1_BASE + S5JS100_TIMER_INT_SEL);
140-
us_ticker_enable_interrupt();
141-
} else {
142-
putreg32(0, S5JS100_TIMER1_BASE + S5JS100_TIMER_LOAD_CON_VALUE);
143-
putreg32(TIMER_TARGET_COUNT_DEFAULT, S5JS100_TIMER1_BASE + S5JS100_TIMER_INT_SEL);
144-
us_ticker_enable_interrupt();
145-
}
127+
uint32_t past_tick = TIMER_TARGET_COUNT_DEFAULT - getreg32(S5JS100_TIMER0_BASE + S5JS100_TIMER_CDC_COUNT_VALUE) - g_us_last_return;
128+
putreg32(timestamp - g_us_last_return - past_tick, S5JS100_TIMER1_BASE + S5JS100_TIMER_LOAD_CON_VALUE);
129+
us_ticker_enable_interrupt();
146130
}
147131

148132
void us_ticker_fire_interrupt(void)

0 commit comments

Comments
 (0)