1 |
786 |
skrzyp |
//==========================================================================
|
2 |
|
|
//
|
3 |
|
|
// flash_load.c
|
4 |
|
|
//
|
5 |
|
|
// RedBoot file/image loader into flash
|
6 |
|
|
//
|
7 |
|
|
//==========================================================================
|
8 |
|
|
// ####ECOSGPLCOPYRIGHTBEGIN####
|
9 |
|
|
// -------------------------------------------
|
10 |
|
|
// This file is part of eCos, the Embedded Configurable Operating System.
|
11 |
|
|
// Copyright (C) 2006 Free Software Foundation, Inc.
|
12 |
|
|
//
|
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
|
16 |
|
|
// version.
|
17 |
|
|
//
|
18 |
|
|
// eCos is distributed in the hope that it will be useful, but WITHOUT
|
19 |
|
|
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
20 |
|
|
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
21 |
|
|
// for more details.
|
22 |
|
|
//
|
23 |
|
|
// You should have received a copy of the GNU General Public License
|
24 |
|
|
// along with eCos; if not, write to the Free Software Foundation, Inc.,
|
25 |
|
|
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
26 |
|
|
//
|
27 |
|
|
// As a special exception, if other files instantiate templates or use
|
28 |
|
|
// macros or inline functions from this file, or you compile this file
|
29 |
|
|
// and link it with other works to produce a work based on this file,
|
30 |
|
|
// this file does not by itself cause the resulting work to be covered by
|
31 |
|
|
// the GNU General Public License. However the source code for this file
|
32 |
|
|
// must still be made available in accordance with section (3) of the GNU
|
33 |
|
|
// General Public License v2.
|
34 |
|
|
//
|
35 |
|
|
// This exception does not invalidate any other reasons why a work based
|
36 |
|
|
// on this file might be covered by the GNU General Public License.
|
37 |
|
|
// -------------------------------------------
|
38 |
|
|
// ####ECOSGPLCOPYRIGHTEND####
|
39 |
|
|
//==========================================================================
|
40 |
|
|
//#####DESCRIPTIONBEGIN####
|
41 |
|
|
//
|
42 |
|
|
// Author(s): Oliver Munz
|
43 |
|
|
// Contributors: om, asl
|
44 |
|
|
// Date: 2006-02-21
|
45 |
|
|
// Purpose:
|
46 |
|
|
// Description:
|
47 |
|
|
//
|
48 |
|
|
// This code is part of RedBoot (tm).
|
49 |
|
|
//
|
50 |
|
|
//####DESCRIPTIONEND####
|
51 |
|
|
//
|
52 |
|
|
//==========================================================================
|
53 |
|
|
|
54 |
|
|
#include <redboot.h>
|
55 |
|
|
#include <flash_load.h>
|
56 |
|
|
|
57 |
|
|
#include <cyg/io/flash.h>
|
58 |
|
|
#include <cyg/infra/cyg_ass.h>
|
59 |
|
|
|
60 |
|
|
static int flash_block_size;
|
61 |
|
|
|
62 |
|
|
static cyg_uint8 * current_flash_page;
|
63 |
|
|
|
64 |
|
|
/* Allocation of the flash-sector size RAM-buffer is done */
|
65 |
|
|
static bool init_done = false;
|
66 |
|
|
|
67 |
|
|
/* We have initialized the current page ready for writing */
|
68 |
|
|
static bool flash_page_init = false;
|
69 |
|
|
|
70 |
|
|
static cyg_uint8 *flash_buffer;
|
71 |
|
|
|
72 |
|
|
// If the io flash code outputs when erasing/writing it will upset the
|
73 |
|
|
// download over the communications channel. So we install a dummy
|
74 |
|
|
// print function.
|
75 |
|
|
static int dummy_printf(const char *fmt, ...){
|
76 |
|
|
return 0;
|
77 |
|
|
}
|
78 |
|
|
|
79 |
|
|
// Calculate the address of the first byte in a flash block
|
80 |
|
|
static cyg_uint8 * flash_block_begin(cyg_uint32 addr)
|
81 |
|
|
{
|
82 |
|
|
return (cyg_uint8 *)
|
83 |
|
|
((addr / flash_block_size) * flash_block_size);
|
84 |
|
|
}
|
85 |
|
|
|
86 |
|
|
// Initialize the loading process
|
87 |
|
|
void flash_load_start(void)
|
88 |
|
|
{
|
89 |
|
|
flash_init(dummy_printf);
|
90 |
|
|
|
91 |
|
|
init_done = true;
|
92 |
|
|
flash_page_init = false;
|
93 |
|
|
}
|
94 |
|
|
|
95 |
|
|
// Write a byte into flash. We maintain a copy in RAM of the FLASH
|
96 |
|
|
// page we are currently "writing" into. This copy is loaded with the
|
97 |
|
|
// current contents of the FLASH page when the first byte is "written"
|
98 |
|
|
// to the page. The "writes" are then made into the RAM copy. We only
|
99 |
|
|
// write to FLASH when there is a "write" outside of the current page,
|
100 |
|
|
// or the flash_load_finish function is called.
|
101 |
|
|
void flash_load_write(cyg_uint8 *flash_addr, cyg_uint8 value)
|
102 |
|
|
{
|
103 |
|
|
|
104 |
|
|
cyg_uint32 retcode = FLASH_ERR_OK;
|
105 |
|
|
void * err_addr;
|
106 |
|
|
cyg_uint32 addr = (cyg_uint32)flash_addr;
|
107 |
|
|
cyg_uint32 offset;
|
108 |
|
|
|
109 |
|
|
if (!flash_page_init) {
|
110 |
|
|
/* First Byte for the current flash block. Read the current contents */
|
111 |
|
|
current_flash_page = flash_block_begin(addr);
|
112 |
|
|
flash_read(flash_buffer, current_flash_page, flash_block_size, &err_addr);
|
113 |
|
|
flash_page_init = true;
|
114 |
|
|
}
|
115 |
|
|
if (flash_block_begin(addr) != current_flash_page) {
|
116 |
|
|
/* We have moved into the next flash page. Write the current
|
117 |
|
|
page so we can move on */
|
118 |
|
|
retcode = flash_erase(current_flash_page, flash_block_size, &err_addr);
|
119 |
|
|
if (retcode != FLASH_ERR_OK){ /* Flash ERROR */
|
120 |
|
|
diag_printf("Error erase at %p: %s\n", err_addr, flash_errmsg(retcode));
|
121 |
|
|
return;
|
122 |
|
|
}
|
123 |
|
|
|
124 |
|
|
retcode = flash_program(current_flash_page, flash_buffer,
|
125 |
|
|
flash_block_size, &err_addr);
|
126 |
|
|
if (retcode != FLASH_ERR_OK){
|
127 |
|
|
diag_printf("Error writing at %p: %s\n",
|
128 |
|
|
err_addr, flash_errmsg(retcode));
|
129 |
|
|
return;
|
130 |
|
|
}
|
131 |
|
|
current_flash_page = flash_block_begin(addr);
|
132 |
|
|
flash_read(flash_buffer, current_flash_page, flash_block_size, &err_addr);
|
133 |
|
|
}
|
134 |
|
|
|
135 |
|
|
offset = flash_addr - current_flash_page;
|
136 |
|
|
CYG_ASSERT(offset < flash_block_size, "offset not inside flash block");
|
137 |
|
|
|
138 |
|
|
flash_buffer[offset] = value;
|
139 |
|
|
}
|
140 |
|
|
|
141 |
|
|
// Program the current page into flash.
|
142 |
|
|
void flash_load_finish(void)
|
143 |
|
|
{
|
144 |
|
|
cyg_uint32 retcode = FLASH_ERR_OK;
|
145 |
|
|
void * err_addr;
|
146 |
|
|
|
147 |
|
|
if (init_done && flash_page_init) {
|
148 |
|
|
flash_page_init = false;
|
149 |
|
|
|
150 |
|
|
retcode = flash_erase(current_flash_page, flash_block_size, &err_addr);
|
151 |
|
|
if (retcode != FLASH_ERR_OK){
|
152 |
|
|
diag_printf("Error erase at %p: %s\n", err_addr, flash_errmsg(retcode));
|
153 |
|
|
} else {
|
154 |
|
|
retcode = flash_program(current_flash_page, flash_buffer,
|
155 |
|
|
flash_block_size, &err_addr);
|
156 |
|
|
if (retcode != FLASH_ERR_OK){
|
157 |
|
|
diag_printf("Error writing at %p: %s\n",
|
158 |
|
|
err_addr, flash_errmsg(retcode));
|
159 |
|
|
}
|
160 |
|
|
}
|
161 |
|
|
}
|
162 |
|
|
flash_init(diag_printf);
|
163 |
|
|
}
|
164 |
|
|
|
165 |
|
|
// This is called during redboot start up. We allocate a buffer the
|
166 |
|
|
// size of the flash page.
|
167 |
|
|
void
|
168 |
|
|
flash_load_init(void)
|
169 |
|
|
{
|
170 |
|
|
int flash_blocks;
|
171 |
|
|
|
172 |
|
|
flash_get_block_info(&flash_block_size, &flash_blocks);
|
173 |
|
|
workspace_end -= flash_block_size;
|
174 |
|
|
|
175 |
|
|
flash_buffer = workspace_end;
|
176 |
|
|
}
|
177 |
|
|
|
178 |
|
|
// Register this initialization function in the table
|
179 |
|
|
RedBoot_init(flash_load_init, RedBoot_INIT_LAST);
|
180 |
|
|
|
181 |
|
|
|
182 |
|
|
|