1 //==========================================================================
5 // RedBoot file/image loader into flash
7 //==========================================================================
8 //####ECOSGPLCOPYRIGHTBEGIN####
9 // -------------------------------------------
10 // This file is part of eCos, the Embedded Configurable Operating System.
11 // Copyright (C) 2006 eCosCentric LTD
13 // eCos is free software; you can redistribute it and/or modify it under
14 // the terms of the GNU General Public License as published by the Free
15 // Software Foundation; either version 2 or (at your option) any later version.
17 // eCos is distributed in the hope that it will be useful, but WITHOUT ANY
18 // WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22 // You should have received a copy of the GNU General Public License along
23 // with eCos; if not, write to the Free Software Foundation, Inc.,
24 // 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
26 // As a special exception, if other files instantiate templates or use macros
27 // or inline functions from this file, or you compile this file and link it
28 // with other works to produce a work based on this file, this file does not
29 // by itself cause the resulting work to be covered by the GNU General Public
30 // License. However the source code for this file must still be made available
31 // in accordance with section (3) of the GNU General Public License.
33 // This exception does not invalidate any other reasons why a work based on
34 // this file might be covered by the GNU General Public License.
36 // Alternative licenses for eCos may be arranged by contacting Red Hat, Inc.
37 // at http://sources.redhat.com/ecos/ecos-license/
38 // -------------------------------------------
39 //####ECOSGPLCOPYRIGHTEND####
40 //==========================================================================
41 //#####DESCRIPTIONBEGIN####
43 // Author(s): Oliver Munz
44 // Contributors: om, asl
49 // This code is part of RedBoot (tm).
51 //####DESCRIPTIONEND####
53 //==========================================================================
56 #include <flash_load.h>
58 #include <cyg/io/flash.h>
59 #include <cyg/infra/cyg_ass.h>
61 static int flash_block_size;
63 static cyg_uint8 * current_flash_page;
65 /* Allocation of the flash-sector size RAM-buffer is done */
66 static bool init_done = false;
68 /* We have initialized the current page ready for writing */
69 static bool flash_page_init = false;
71 static cyg_uint8 *flash_buffer;
73 // If the io flash code outputs when erasing/writing it will upset the
74 // download over the communications channel. So we install a dummy
76 static int dummy_printf(const char *fmt, ...){
80 // Calculate the address of the first byte in a flash block
81 static cyg_uint8 * flash_block_begin(cyg_uint32 addr)
84 ((addr / flash_block_size) * flash_block_size);
87 // Initialize the loading process
88 void flash_load_start(void)
90 flash_init(dummy_printf);
93 flash_page_init = false;
96 // Write a byte into flash. We maintain a copy in RAM of the FLASH
97 // page we are currently "writing" into. This copy is loaded with the
98 // current contents of the FLASH page when the first byte is "written"
99 // to the page. The "writes" are then made into the RAM copy. We only
100 // write to FLASH when there is a "write" outside of the current page,
101 // or the flash_load_finish function is called.
102 void flash_load_write(cyg_uint8 *flash_addr, cyg_uint8 value)
105 cyg_uint32 retcode = FLASH_ERR_OK;
107 cyg_uint32 addr = (cyg_uint32)flash_addr;
110 if (!flash_page_init) {
111 /* First Byte for the current flash block. Read the current contents */
112 current_flash_page = flash_block_begin(addr);
113 flash_read(flash_buffer, current_flash_page, flash_block_size, &err_addr);
114 flash_page_init = true;
116 if (flash_block_begin(addr) != current_flash_page) {
117 /* We have moved into the next flash page. Write the current
118 page so we can move on */
119 retcode = flash_erase(current_flash_page, flash_block_size, &err_addr);
120 if (retcode != FLASH_ERR_OK){ /* Flash ERROR */
121 diag_printf("Error erase at %p: %s\n", err_addr, flash_errmsg(retcode));
125 retcode = flash_program(current_flash_page, flash_buffer,
126 flash_block_size, &err_addr);
127 if (retcode != FLASH_ERR_OK){
128 diag_printf("Error writing at %p: %s\n",
129 err_addr, flash_errmsg(retcode));
132 current_flash_page = flash_block_begin(addr);
133 flash_read(flash_buffer, current_flash_page, flash_block_size, &err_addr);
136 offset = flash_addr - current_flash_page;
137 CYG_ASSERT(offset < flash_block_size, "offset not inside flash block");
139 flash_buffer[offset] = value;
142 // Program the current page into flash.
143 void flash_load_finish(void)
145 cyg_uint32 retcode = FLASH_ERR_OK;
148 if (init_done && flash_page_init) {
149 flash_page_init = false;
151 retcode = flash_erase(current_flash_page, flash_block_size, &err_addr);
152 if (retcode != FLASH_ERR_OK){
153 diag_printf("Error erase at %p: %s\n", err_addr, flash_errmsg(retcode));
155 retcode = flash_program(current_flash_page, flash_buffer,
156 flash_block_size, &err_addr);
157 if (retcode != FLASH_ERR_OK){
158 diag_printf("Error writing at %p: %s\n",
159 err_addr, flash_errmsg(retcode));
163 flash_init(diag_printf);
166 // This is called during redboot start up. We allocate a buffer the
167 // size of the flash page.
169 flash_load_init(void)
173 flash_get_block_info(&flash_block_size, &flash_blocks);
174 workspace_end -= flash_block_size;
176 flash_buffer = workspace_end;
179 // Register this initialization function in the table
180 RedBoot_init(flash_load_init, RedBoot_INIT_LAST);