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, ...)
81 // Calculate the address of the first byte in a flash block
82 static cyg_uint8 *flash_block_begin(cyg_uint32 addr)
85 ((addr / flash_block_size) * flash_block_size);
88 // Initialize the loading process
89 void flash_load_start(void)
91 flash_init(dummy_printf);
94 flash_page_init = false;
97 // Write a byte into flash. We maintain a copy in RAM of the FLASH
98 // page we are currently "writing" into. This copy is loaded with the
99 // current contents of the FLASH page when the first byte is "written"
100 // to the page. The "writes" are then made into the RAM copy. We only
101 // write to FLASH when there is a "write" outside of the current page,
102 // or the flash_load_finish function is called.
103 void flash_load_write(cyg_uint8 *flash_addr, cyg_uint8 value)
106 cyg_uint32 retcode = FLASH_ERR_OK;
108 cyg_uint32 addr = (cyg_uint32)flash_addr;
111 if (!flash_page_init) {
112 /* First Byte for the current flash block. Read the current contents */
113 current_flash_page = flash_block_begin(addr);
114 flash_read(flash_buffer, current_flash_page, flash_block_size, &err_addr);
115 flash_page_init = true;
117 if (flash_block_begin(addr) != current_flash_page) {
118 /* We have moved into the next flash page. Write the current
119 page so we can move on */
120 retcode = flash_erase(current_flash_page, flash_block_size, &err_addr);
121 if (retcode != FLASH_ERR_OK){ /* Flash ERROR */
122 diag_printf("Error erase at %p: %s\n", err_addr, flash_errmsg(retcode));
126 retcode = flash_program(current_flash_page, flash_buffer,
127 flash_block_size, &err_addr);
128 if (retcode != FLASH_ERR_OK){
129 diag_printf("Error writing at %p: %s\n",
130 err_addr, flash_errmsg(retcode));
133 current_flash_page = flash_block_begin(addr);
134 flash_read(flash_buffer, current_flash_page, flash_block_size, &err_addr);
137 offset = flash_addr - current_flash_page;
138 CYG_ASSERT(offset < flash_block_size, "offset not inside flash block");
140 flash_buffer[offset] = value;
143 // Program the current page into flash.
144 void flash_load_finish(void)
146 cyg_uint32 retcode = FLASH_ERR_OK;
149 if (init_done && flash_page_init) {
150 flash_page_init = false;
152 retcode = flash_erase(current_flash_page, flash_block_size, &err_addr);
153 if (retcode != FLASH_ERR_OK){
154 diag_printf("Error erase at %p: %s\n", err_addr, flash_errmsg(retcode));
156 retcode = flash_program(current_flash_page, flash_buffer,
157 flash_block_size, &err_addr);
158 if (retcode != FLASH_ERR_OK){
159 diag_printf("Error writing at %p: %s\n",
160 err_addr, flash_errmsg(retcode));
164 flash_init(diag_printf);
167 // This is called during redboot start up. We allocate a buffer the
168 // size of the flash page.
170 flash_load_init(void)
174 flash_get_block_info(&flash_block_size, &flash_blocks);
175 workspace_end -= flash_block_size;
177 flash_buffer = workspace_end;
180 // Register this initialization function in the table
181 RedBoot_init(flash_load_init, RedBoot_INIT_LAST);