Skip to content

Commit a685935

Browse files
committed
Move functions to RAM for flash operation
1 parent 3be63df commit a685935

File tree

12 files changed

+551
-344
lines changed

12 files changed

+551
-344
lines changed

IDE/Renesas/e2studio/RX72N/app_RenesasRX01/.cproject

Lines changed: 41 additions & 35 deletions
Large diffs are not rendered by default.

IDE/Renesas/e2studio/RX72N/app_RenesasRX01/.project

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,5 +44,10 @@
4444
<type>1</type>
4545
<locationURI>PARENT-5-PROJECT_LOC/hal/renesas-rx.c</locationURI>
4646
</link>
47+
<link>
48+
<name>src/string.c</name>
49+
<type>1</type>
50+
<locationURI>PARENT-5-PROJECT_LOC/src/string.c</locationURI>
51+
</link>
4752
</linkedResources>
4853
</projectDescription>

IDE/Renesas/e2studio/RX72N/app_RenesasRX01/app_RenesasRX01.scfg

Lines changed: 236 additions & 236 deletions
Large diffs are not rendered by default.

IDE/Renesas/e2studio/RX72N/app_RenesasRX01/src/app_RenesasRX01.c

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@
2727
#include <string.h>
2828
#include "hal.h"
2929
#include "wolfboot/wolfboot.h"
30-
30+
#include "image.h"
31+
#include "r_smc_entry.h"
3132
static const char* state2str(uint8_t s)
3233
{
3334
switch(s) {
@@ -39,6 +40,17 @@ static const char* state2str(uint8_t s)
3940
}
4041
}
4142

43+
static const char* upFlag2str(uint8_t s)
44+
{
45+
switch(s) {
46+
case SECT_FLAG_NEW: return "New";
47+
case SECT_FLAG_SWAPPING: return "Swapping";
48+
case SECT_FLAG_BACKUP: return "Backup";
49+
case SECT_FLAG_UPDATED: return "Updated";
50+
default: return "Unknown";
51+
}
52+
}
53+
4254
static void printPart(uint8_t *part)
4355
{
4456
#ifdef WOLFBOOT_DEBUG_PARTION
@@ -47,16 +59,21 @@ static void printPart(uint8_t *part)
4759
#endif
4860
uint8_t *magic;
4961
uint8_t state;
62+
uint8_t upflag;
5063
uint32_t ver;
5164

5265
magic = part;
5366
printf("Magic: %c%c%c%c\n", magic[0], magic[1], magic[2], magic[3]);
5467
ver = wolfBoot_get_blob_version(part);
5568
printf("Version: %02x\n", ver);
56-
state = *(part + WOLFBOOT_PARTITION_SIZE - sizeof(uint32_t) - 1);
69+
wolfBoot_get_partition_state(0, &state);
5770
printf("Status: %02x (%s)\n", state,state2str(state));
5871
magic = part + WOLFBOOT_PARTITION_SIZE - sizeof(uint32_t);
72+
if (magic[0] != 0x42)
73+
magic = part + WOLFBOOT_PARTITION_SIZE - WOLFBOOT_SECTOR_SIZE - sizeof(uint32_t);
5974
printf("Trailer Mgc: %c%c%c%c\n", magic[0], magic[1], magic[2], magic[3]);
75+
wolfBoot_get_update_sector_flag(0, &upflag);
76+
printf("Update flag: %02x (%s)\n", upflag, upFlag2str(upflag));
6077

6178
#ifdef WOLFBOOT_DEBUG_PARTION
6279
v = (uint32_t *)part;
@@ -68,7 +85,18 @@ static void printPart(uint8_t *part)
6885
#endif
6986

7087
}
71-
88+
#ifdef WOLFBOOT_DEBUG_PARTION
89+
static void verify_flash_write(uint32_t addr, int len)
90+
{
91+
uint8_t *p = (uint8_t *)addr;
92+
int i;
93+
printf("verify addr=0x%08x: ", addr);
94+
for (i = 0; i < len && i < 8; i++) {
95+
printf("%02x ", p[i]);
96+
}
97+
printf("\n");
98+
}
99+
#endif
72100

73101
static void printPartitions(void)
74102
{

IDE/Renesas/e2studio/RX72N/include/user_settings.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,5 +278,10 @@
278278
#else
279279
# define WOLFSSL_SMALL_STACK
280280
#endif
281-
281+
#define PRINTF_ENABLED
282+
#define NVM_FLASH_WRITEONCE
283+
#ifdef XMEMCPY
284+
#undef XMEMCPY
285+
#define XMEMCPY ram_memcpy
286+
#endif
282287
#endif /* !H_USER_SETTINGS_ */

IDE/Renesas/e2studio/RX72N/resouce/section_info_SWuse.esi

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
<?xml version="1.0" encoding="ASCII"?>
22
<com.renesas.linkersection.model:SectionContainer xmi:version="2.0" xmlns:xmi="http://www.omg.org/XMI" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:com.renesas.linkersection.model="http:///LinkerSection.ecore">
3-
<sections name="SU">
3+
<sections name="SI">
44
<sectionAddress xsi:type="com.renesas.linkersection.model:FixedAddress" fixedAddress="4"/>
55
</sections>
6-
<sections name="SI"/>
76
<sections name="B_1"/>
87
<sections name="R_1"/>
98
<sections name="B_2"/>
@@ -13,9 +12,12 @@
1312
<sections name="B_8"/>
1413
<sections name="R_8"/>
1514
<sections name="RPFRAM"/>
16-
<sections name="RPFRAM2"/>
15+
<sections name="C_FIRMWARE_UPDATE_CONTROL_BLOCK">
16+
<sectionAddress xsi:type="com.renesas.linkersection.model:FixedAddress" fixedAddress="1048576"/>
17+
</sections>
18+
<sections name="C_FIRMWARE_UPDATE_CONTROL_BLOCK_MIRROR"/>
1719
<sections name="PResetPRG">
18-
<sectionAddress xsi:type="com.renesas.linkersection.model:FixedAddress" fixedAddress="4290839040"/>
20+
<sectionAddress xsi:type="com.renesas.linkersection.model:FixedAddress" fixedAddress="4290772992"/>
1921
</sections>
2022
<sections name="C_1"/>
2123
<sections name="C_2"/>
@@ -27,7 +29,6 @@
2729
<sections name="L"/>
2830
<sections name="P"/>
2931
<sections name="PFRAM"/>
30-
<sections name="PFRAM2"/>
3132
<sections name="EXCEPTVECT">
3233
<sectionAddress xsi:type="com.renesas.linkersection.model:FixedAddress" fixedAddress="4294967168"/>
3334
</sections>

IDE/Renesas/e2studio/RX72N/wolfBoot/.cproject

Lines changed: 106 additions & 36 deletions
Large diffs are not rendered by default.

hal/renesas-rx.c

Lines changed: 59 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -485,8 +485,23 @@ void hal_prepare_boot(void)
485485

486486
}
487487

488+
#ifdef __CCRX__
489+
/* copy RAM functions from ROM to RAM */
490+
static void copyfuncs(void)
491+
{
492+
unsigned char *dst, *src;
493+
src = __sectop("PFRAM");
494+
dst = __sectop("RPFRAM");
495+
while(src < __secend("PFRAM")) {
496+
*dst++ = *src++;
497+
}
498+
}
499+
#endif
488500
int hal_flash_init(void)
489501
{
502+
#ifdef __CCRX__
503+
copyfuncs();
504+
#endif
490505
/* Flash Write Enable */
491506
FLASH_FWEPROR = FLASH_FWEPROR_FLWE;
492507

@@ -506,23 +521,41 @@ int hal_flash_init(void)
506521
/* write up to 128 bytes at a time */
507522
#define FLASH_FACI_CODE_BLOCK_SZ \
508523
(FLASH_FACI_CMD_PROGRAM_CODE_LENGTH * FLASH_FACI_CMD_PROGRAM_DATA_LENGTH)
524+
#define FLASH_OK 0
525+
#define FLASH_ERR_ALIGN -1
526+
#define FLASH_ERR_PRG -2
527+
#define FLASH_ERR_ILGL -3
528+
#define FLASH_ERR_ERS -4
529+
#ifdef __CCRX__
530+
#pragma section FRAM
531+
#define MEMCPY ram_memcpy
532+
#else
533+
#define MEMCPY memcpy
534+
#endif
509535
int RAMFUNCTION hal_flash_write(uint32_t addr, const uint8_t *data, int len)
510536
{
511-
int ret, i, chunk;
512-
uint8_t codeblock[FLASH_FACI_CODE_BLOCK_SZ];
537+
int i;
538+
uint8_t codeblock[FLASH_FACI_CODE_BLOCK_SZ] = {0};
513539
uint16_t* data16 = (uint16_t*)data;
540+
uint32_t block_base;
541+
uint32_t offset;
542+
int write_size;
514543

515544
while (len > 0) {
516-
/* handle partial remainder */
517-
if (len < FLASH_FACI_CODE_BLOCK_SZ) {
518-
uint8_t *src = (uint8_t*)addr;
519-
int remain = FLASH_FACI_CODE_BLOCK_SZ - len;
520-
memcpy(codeblock, data16, len);
521-
memcpy(codeblock + len, src + len, remain);
522-
data16 = (uint16_t*)codeblock;
523-
}
545+
/* Align address to 128-byte boundary */
546+
block_base = addr & ~(FLASH_FACI_CODE_BLOCK_SZ - 1);
547+
offset = addr - block_base;
548+
549+
MEMCPY(codeblock, (uint8_t*)block_base, FLASH_FACI_CODE_BLOCK_SZ);
550+
write_size = FLASH_FACI_CODE_BLOCK_SZ - offset;
551+
if (write_size > len)
552+
write_size = len;
524553

525-
FLASH_FSADDR = addr;
554+
MEMCPY(&codeblock[offset], data, write_size);
555+
data16 = (uint16_t*)codeblock;
556+
557+
558+
FLASH_FSADDR = block_base;
526559
/* flash program command */
527560
FLASH_FACI_CMD8 = FLASH_FACI_CMD_PROGRAM;
528561
/* number of 16-bit blocks: for code blocks is always 0x40 (64) */
@@ -539,9 +572,18 @@ int RAMFUNCTION hal_flash_write(uint32_t addr, const uint8_t *data, int len)
539572

540573
/* Wait for FCU operation to complete */
541574
while ((FLASH_FSTATR & FLASH_FSTATR_FRDY) == 0);
542-
543-
len -= FLASH_FACI_CODE_BLOCK_SZ;
544-
addr += FLASH_FACI_CODE_BLOCK_SZ;
575+
if (FLASH_FSTATR & FLASH_FSTATR_ILGLERR) {
576+
return FLASH_ERR_ILGL;
577+
}
578+
if (FLASH_FSTATR & FLASH_FSTATR_PRGERR) {
579+
return FLASH_ERR_PRG;
580+
}
581+
if (FLASH_FSTATR & FLASH_FSTATR_ERSERR) {
582+
return FLASH_ERR_PRG;
583+
}
584+
len -= write_size;
585+
addr += write_size;
586+
data += write_size;
545587
}
546588
return 0;
547589
}
@@ -639,7 +681,9 @@ void RAMFUNCTION hal_flash_lock(void)
639681
FLASH_FENTRYR_CODE_READ | FLASH_FENTRYR_DATA_READ);
640682
return;
641683
}
642-
684+
#ifdef __CCRX__
685+
#pragma section
686+
#endif
643687
#if !defined(WOLFBOOT_NO_PARTITIONS) && !defined(TARGET_library)
644688
void* hal_get_primary_address(void)
645689
{

src/boot_renesas.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,16 @@ static void longJump(const uint32_t *app_offset)
5555
jmp r1;
5656
}
5757
#endif
58+
static void verify_flash_write(uint32_t addr, int len)
59+
{
60+
uint8_t *p = (uint8_t *)addr;
61+
int i;
62+
printf("verify addr=0x%08x: ", addr);
63+
for (i = 0; i < len && i < 8; i++) {
64+
printf("%02x ", p[i]);
65+
}
66+
printf("\n");
67+
}
5868

5969
/* Calls the application entry point */
6070
void do_boot(const uint32_t *app_offset)
@@ -84,6 +94,7 @@ void do_boot(const uint32_t *app_offset)
8494
#if defined(__RX__)
8595
/* Do unconditional jump (r1 = app_offset) */
8696
#if defined(__CCRX__)
97+
printf("app_offset 0x%p\n", app_offset);
8798
longJump(app_offset);
8899
#else
89100
app_entry = (void(*))(*app_offset);

src/libwolfboot.c

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,9 @@ void WEAKFUNCTION hal_cache_invalidate(void)
218218
{
219219
/* if cache flushing is required implement in hal */
220220
}
221-
221+
#ifdef __CCRX__
222+
#pragma section FRAM
223+
#endif
222224
static int RAMFUNCTION nvm_select_fresh_sector(int part)
223225
{
224226
int sel;
@@ -377,7 +379,9 @@ static int RAMFUNCTION partition_magic_write(uint8_t part, uintptr_t addr)
377379
ret = hal_flash_erase(addr_read, WOLFBOOT_SECTOR_SIZE);
378380
return ret;
379381
}
380-
382+
#ifdef __CCRX__
383+
#pragma section
384+
#endif
381385
#else
382386
# define trailer_write(part,addr, val) hal_flash_write(addr, (void *)&val, 1)
383387
# define partition_magic_write(part,addr) hal_flash_write(addr, \
@@ -395,6 +399,9 @@ void RAMFUNCTION set_trailer_at(uint8_t part, uint32_t at, uint8_t val);
395399
void RAMFUNCTION set_partition_magic(uint8_t part);
396400

397401
#elif !defined(WOLFBOOT_FIXED_PARTITIONS)
402+
#ifdef __CCRX__
403+
#pragma section FRAM
404+
#endif
398405
static uint8_t* RAMFUNCTION get_trailer_at(uint8_t part, uint32_t at)
399406
{
400407
(void)part;
@@ -413,9 +420,13 @@ static void RAMFUNCTION set_partition_magic(uint8_t part)
413420
(void)part;
414421
return;
415422
}
416-
423+
#ifdef __CCRX__
424+
#pragma section
425+
#endif
417426
#else
418-
427+
#ifdef __CCRX__
428+
#pragma section FRAM
429+
#endif
419430
/**
420431
* @brief Get the trailer at a specific address
421432
*
@@ -550,12 +561,18 @@ static void RAMFUNCTION set_partition_magic(uint8_t part)
550561
}
551562
}
552563
}
564+
#ifdef __CCRX__
565+
#pragma section
566+
#endif
553567
#endif
554568
#endif /* !MOCK_PARTITION_TRAILER */
555569

556570

557571

558572
#ifdef WOLFBOOT_FIXED_PARTITIONS
573+
#ifdef __CCRX__
574+
#pragma section FRAM
575+
#endif
559576
/**
560577
* @brief Get the magic trailer of a partition.
561578
*
@@ -843,6 +860,9 @@ void RAMFUNCTION wolfBoot_success(void)
843860
wolfBoot_erase_encrypt_key();
844861
#endif
845862
}
863+
#ifdef __CCRX__
864+
#pragma section
865+
#endif
846866
#endif /* WOLFBOOT_FIXED_PARTITIONS */
847867

848868
/**

0 commit comments

Comments
 (0)