Hi,
I have implemented my own bootloader and need to add support for securing the flash, by writing to the flash configuration field,
and the FSEC field in particular. I am using IAR EWARM 8.11.
I suppose that I could get around this with doing the same as the AN2295 bootloader does:
#pragma section = "FlashConfig"
#pragma location = "FlashConfig"
__root const FlashConfig_t Config =
{
0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFE,
};
and then changing 0xFFFFFFFE to 0xFFFFFFFF.
I have some severe problems in adapting my current ICF/linker file with the one for AN2295, so that I can write to the 0x400-0x40F range. I have some problems figuring out the .icf syntax, and couldn't find it documented anywhere.
I have reserved the flash from 0x410 to 0x5FFF bytes for the bootloader.
This is the original ICF file:
define symbol __ICFEDIT_intvec_start__ = 0x00000000;
/*-Memory Regions-*/
define symbol __ICFEDIT_region_ROM_start__ = 0x00000000;
define symbol __ICFEDIT_region_ROM_end__ = 0x00005FFF;
define symbol __ICFEDIT_region_RAM_start__ = 0x1FFFC410;
define symbol __ICFEDIT_region_RAM_end__ = 0x1FFFFFFF;
/*-Sizes-*/
define symbol __ICFEDIT_size_cstack__ = 0x1000;
define symbol __ICFEDIT_size_heap__ = 0x200;
/**** End of ICF editor section. ###ICF###*/define symbol __region_RAM2_start__ = 0x20000000;
define symbol __region_RAM2_end__ = 0x2000FFFF;define exported symbol __VECTOR_TABLE = 0x00000000;
define exported symbol __VECTOR_RAM = 0x1FFF0000;define symbol __code_start__ = 0x00000410;
define memory mem with size = 4G;
define region ROM_region = mem:[from __ICFEDIT_region_ROM_start__ to __ICFEDIT_region_ROM_end__];
define region RAM_region = mem:[from __ICFEDIT_region_RAM_start__ to __ICFEDIT_region_RAM_end__] | mem:[from __region_RAM2_start__ to __region_RAM2_end__];define block CSTACK with alignment = 8, size = __ICFEDIT_size_cstack__ { };
define block HEAP with alignment = 8, size = __ICFEDIT_size_heap__ { };initialize manually { readwrite };
initialize manually { section .data};
initialize manually { section .textrw_init };
initialize manually { section .textrw };
do not initialize { section .noinit };define block CodeRelocate { section .textrw_init };
define block CodeRelocateRam { section .textrw };place at address mem:__ICFEDIT_intvec_start__ { readonly section .intvec };
place at address mem:__code_start__ { readonly section .noinit };place in ROM_region { readonly, block CodeRelocate};
place in RAM_region { readwrite, block CodeRelocateRam, block CSTACK, block HEAP };
This is my attempt at modifying it:
define symbol __ICFEDIT_intvec_start__ = 0x00000000;
/*-Memory Regions-*/
define symbol __ICFEDIT_region_ROM_start__ = 0x00000000;
define symbol __ICFEDIT_region_ROM_end__ = 0x00005FFF;
define symbol __ICFEDIT_region_RAM_start__ = 0x1FFF0410;
define symbol __ICFEDIT_region_RAM_end__ = 0x1FFFFFFF;
/*-Sizes-*/
define symbol __ICFEDIT_size_cstack__ = 0x1000;
define symbol __ICFEDIT_size_heap__ = 0x200;
/**** End of ICF editor section. ###ICF###*/define symbol __region_RAM2_start__ = 0x20000000;
define symbol __region_RAM2_end__ = 0x2000FFFF;define exported symbol __VECTOR_TABLE = 0x00000000;
define exported symbol __VECTOR_RAM = 0x1FFF0000;
define symbol FlashConfig_start__ = 0x00000400;
define symbol FlashConfig_end__ = 0x0000040F;define symbol __code_start__ = 0x00000410;
define memory mem with size = 4G;
define region ROM_region = mem:[from __ICFEDIT_region_ROM_start__ to (FlashConfig_start__-1)] | [from (FlashConfig_end__+1) to __ICFEDIT_region_ROM_end__];
define region RAM_region = mem:[from __ICFEDIT_region_RAM_start__ to __ICFEDIT_region_RAM_end__] | mem:[from __region_RAM2_start__ to __region_RAM2_end__];
define region FlashConfig_region = mem:[from FlashConfig_start__ to FlashConfig_end__];define block CSTACK with alignment = 8, size = __ICFEDIT_size_cstack__ { };
define block HEAP with alignment = 8, size = __ICFEDIT_size_heap__ { };initialize manually { readwrite };
initialize manually { section .data};
initialize manually { section .textrw };
do not initialize { section .FlashConfig };
do not initialize { section .noinit };
define block CodeRelocate { section .textrw_init };
define block CodeRelocateRam { section .textrw };place at address mem:__ICFEDIT_intvec_start__ { readonly section .intvec };
place at address mem:__code_start__ { readonly section .noinit };place in ROM_region { readonly, block CodeRelocate};
place in RAM_region { readwrite, block CodeRelocateRam, block CSTACK, block HEAP };
place in FlashConfig_region { section FlashConfig};
IAR couldn't find space for all variables as shown in the attached image, and I just can't see what causes this. Any help/pointers?
Hello Troels Oesteraa,
If you want to modify the FSEC in the IAR project, you need to modify two points.
Now, let's take KBOOT2.0 freedom_bootloader for FRDM-K22F120M board as an example, it can be downloaded from this link:
1. Modify the flash configuration field.
It is in the startup_MK22F51212.S
2. Modify the IAR options
Because the IAR already protect the flash configuration field, then if you want to modify the FOPT, you need to modify the options in IAR IDE. Project->options, click the below edit button.
Enter :” --enable_config_write “ in Flash loader configuration windows.
Save the .board file as new.board
Then you will find you choose the new.board.
Now build the project and download the code to your board, the flash configuration field will be modified.
Wish it helps you!
If you still have question, please let me know!
Have a great day,
Kerry
-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------
Hi Kerry,
First and foremost, thank you for your quick reply.
I did initially find other post mentioning the same IAR options with the "--enable_config_write". However, when in my project, I have never even ticked the flag in "Use flash loader(s)" since my debug/JTAG tools apparently handles this (J-Flash unit). In production, I think I will use EzPort to download the bootloader to the flash, where the bootloader will be a compiled hex file from IAR, which enables security at startup. Hopefully by letting the startup files write the FSEC parts, this will be compiled correctly into the hex-files too.
So should I still enable flash loader with the additional option? If so, what would the recommended stock .board file for a K22FN1M0VLL12 be?
I tried the KBOOT v2, and must say I would like to figure out how the startup works from line-to-line. I can understand that it writes the interrupt vectors and everything, which I had already done in C, so I would like to keep that.
So I put a "Section:FlashConfig:CODE" above the __FlashConfig label, but receive compiler warning "Warning [410]: DC or DS directive while in code area."
That is because it is not ":CODE" but something else, but what? It can't be .intvect either as that would not be the right section.
I have attached my current ICF file and startup.s to this post, just so we're on the same page.
Hello customer,
1. If you want to use IAR to modify the flash configration filed
You need to add "--enable_config_write" in options, the board file, you can use choose it from the iar install path.
2. If you are using the complied hex file, I think you also can modify the FSEC filed directly in the hex file, but take care, please also modify the according hex line check code.
3. About your .s and link file.
It seems your are not using the startup_MK22xx.s like the kboot target code.
But don't worry, you also can refer to the K60 old sample code, define it following the vector table.
1) define the .intvec
You can refer to my attached .icf file
define symbol __ICFEDIT_intvec_start__ = 0x00000000;
place at address mem:__ICFEDIT_intvec_start__ { readonly section .intvec };
2) in the vectors.c
typedef void (*vector_entry)(void);
#if defined(IAR)
#pragma location = ".intvec"
const vector_entry __vector_table[] = //@ ".intvec" =
#elif defined(CW)
#pragma define_section vectortable ".vectortable" ".vectortable" ".vectortable" far_abs R
#define VECTOR __declspec(vectortable)
const VECTOR vector_entry __vector_table[] = //@ ".intvec" =
#endif
{
VECTOR_000, /* Initial SP */
VECTOR_001, /* Initial PC */
VECTOR_002,
VECTOR_003,
VECTOR_004,
VECTOR_005,
VECTOR_006,
VECTOR_007,
VECTOR_008,
VECTOR_009,
VECTOR_010,
VECTOR_011,
VECTOR_012,
VECTOR_013,
VECTOR_014,
VECTOR_015,
VECTOR_016,
VECTOR_017,
VECTOR_018,
VECTOR_019,
VECTOR_020,
VECTOR_021,
VECTOR_022,
VECTOR_023,
VECTOR_024,
VECTOR_025,
VECTOR_026,
VECTOR_027,
VECTOR_028,
VECTOR_029,
VECTOR_030,
VECTOR_031,
VECTOR_032,
VECTOR_033,
VECTOR_034,
VECTOR_035,
VECTOR_036,
VECTOR_037,
VECTOR_038,
VECTOR_039,
VECTOR_040,
VECTOR_041,
VECTOR_042,
VECTOR_043,
VECTOR_044,
VECTOR_045,
VECTOR_046,
VECTOR_047,
VECTOR_048,
VECTOR_049,
VECTOR_050,
VECTOR_051,
VECTOR_052,
VECTOR_053,
VECTOR_054,
VECTOR_055,
VECTOR_056,
VECTOR_057,
VECTOR_058,
VECTOR_059,
VECTOR_060,
VECTOR_061,
VECTOR_062,
VECTOR_063,
VECTOR_064,
VECTOR_065,
VECTOR_066,
VECTOR_067,
VECTOR_068,
VECTOR_069,
VECTOR_070,
VECTOR_071,
VECTOR_072,
VECTOR_073,
VECTOR_074,
VECTOR_075,
VECTOR_076,
VECTOR_077,
VECTOR_078,
VECTOR_079,
VECTOR_080,
VECTOR_081,
VECTOR_082,
VECTOR_083,
VECTOR_084,
VECTOR_085,
VECTOR_086,
VECTOR_087,
VECTOR_088,
VECTOR_089,
VECTOR_090,
VECTOR_091,
VECTOR_092,
VECTOR_093,
VECTOR_094,
VECTOR_095,
VECTOR_096,
VECTOR_097,
VECTOR_098,
VECTOR_099,
VECTOR_100,
VECTOR_101,
VECTOR_102,
VECTOR_103,
VECTOR_104,
VECTOR_105,
VECTOR_106,
VECTOR_107,
VECTOR_108,
VECTOR_109,
VECTOR_110,
VECTOR_111,
VECTOR_112,
VECTOR_113,
VECTOR_114,
VECTOR_115,
VECTOR_116,
VECTOR_117,
VECTOR_118,
VECTOR_119,
VECTOR_120,
VECTOR_121,
VECTOR_122,
VECTOR_123,
VECTOR_124,
VECTOR_125,
VECTOR_126,
VECTOR_127,
VECTOR_128,
VECTOR_129,
VECTOR_130,
VECTOR_131,
VECTOR_132,
VECTOR_133,
VECTOR_134,
VECTOR_135,
VECTOR_136,
VECTOR_137,
VECTOR_138,
VECTOR_139,
VECTOR_140,
VECTOR_141,
VECTOR_142,
VECTOR_143,
VECTOR_144,
VECTOR_145,
VECTOR_146,
VECTOR_147,
VECTOR_148,
VECTOR_149,
VECTOR_150,
VECTOR_151,
VECTOR_152,
VECTOR_153,
VECTOR_154,
VECTOR_155,
VECTOR_156,
VECTOR_157,
VECTOR_158,
VECTOR_159,
VECTOR_160,
VECTOR_161,
VECTOR_162,
VECTOR_163,
VECTOR_164,
VECTOR_165,
VECTOR_166,
VECTOR_167,
VECTOR_168,
VECTOR_169,
VECTOR_170,
VECTOR_171,
VECTOR_172,
VECTOR_173,
VECTOR_174,
VECTOR_175,
VECTOR_176,
VECTOR_177,
VECTOR_178,
VECTOR_179,
VECTOR_180,
VECTOR_181,
VECTOR_182,
VECTOR_183,
VECTOR_184,
VECTOR_185,
VECTOR_186,
VECTOR_187,
VECTOR_188,
VECTOR_189,
VECTOR_190,
VECTOR_191,
VECTOR_192,
VECTOR_193,
VECTOR_194,
VECTOR_195,
VECTOR_196,
VECTOR_197,
VECTOR_198,
VECTOR_199,
VECTOR_200,
VECTOR_201,
VECTOR_202,
VECTOR_203,
VECTOR_204,
VECTOR_205,
VECTOR_206,
VECTOR_207,
VECTOR_208,
VECTOR_209,
VECTOR_210,
VECTOR_211,
VECTOR_212,
VECTOR_213,
VECTOR_214,
VECTOR_215,
VECTOR_216,
VECTOR_217,
VECTOR_218,
VECTOR_219,
VECTOR_220,
VECTOR_221,
VECTOR_222,
VECTOR_223,
VECTOR_224,
VECTOR_225,
VECTOR_226,
VECTOR_227,
VECTOR_228,
VECTOR_229,
VECTOR_230,
VECTOR_231,
VECTOR_232,
VECTOR_233,
VECTOR_234,
VECTOR_235,
VECTOR_236,
VECTOR_237,
VECTOR_238,
VECTOR_239,
VECTOR_240,
VECTOR_241,
VECTOR_242,
VECTOR_243,
VECTOR_244,
VECTOR_245,
VECTOR_246,
VECTOR_247,
VECTOR_248,
VECTOR_249,
VECTOR_250,
VECTOR_251,
VECTOR_252,
VECTOR_253,
VECTOR_254,
VECTOR_255,
CONFIG_1, /* Flash configuration field values */
CONFIG_2, /* Flash configuration field values */
CONFIG_3, /* Flash configuration field values */
CONFIG_4, /* Flash configuration field values */
};
in vectors.h
#define CONFIG_1 (pointer*)0xffffffff
#define CONFIG_2 (pointer*)0xffffffff
#define CONFIG_3 (pointer*)0xffffffff
#define CONFIG_4 (pointer*)0xfffffBfe //0xfffffffe
More details, please refer to my attached file.
Wish it helps you!
Have a great day,
Kerry
-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------