]> git.kernelconcepts.de Git - karo-tx-redboot.git/blob - packages/redboot/v2_0/src/flash_load.c
unified MX27, MX25, MX37 trees
[karo-tx-redboot.git] / packages / redboot / v2_0 / src / flash_load.c
1 //==========================================================================
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 eCosCentric LTD
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 version.
16 //
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
20 // for more details.
21 //
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.
25 //
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.
32 //
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.
35 //
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####
42 //
43 // Author(s):    Oliver Munz
44 // Contributors: om, asl
45 // Date:         2006-02-21
46 // Purpose:
47 // Description:
48 //
49 // This code is part of RedBoot (tm).
50 //
51 //####DESCRIPTIONEND####
52 //
53 //==========================================================================
54
55 #include <redboot.h>
56 #include <flash_load.h>
57
58 #include <cyg/io/flash.h>
59 #include <cyg/infra/cyg_ass.h>
60
61 static int flash_block_size;
62
63 static cyg_uint8 *current_flash_page;
64
65 /* Allocation of the flash-sector size RAM-buffer is done */
66 static bool init_done = false;
67
68 /* We have initialized the current page ready for writing */
69 static bool flash_page_init = false;
70
71 static cyg_uint8 *flash_buffer;
72
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
75 // print function.
76 static int dummy_printf(const char *fmt, ...)
77 {
78         return 0;
79 }
80
81 // Calculate the address of the first byte in a flash block
82 static cyg_uint8 *flash_block_begin(cyg_uint32 addr)
83 {
84         return (cyg_uint8 *)
85                 ((addr / flash_block_size) * flash_block_size);
86 }
87
88 // Initialize the loading process
89 void flash_load_start(void)
90 {
91         flash_init(dummy_printf);
92
93         init_done = true;
94         flash_page_init = false;
95 }
96
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)
104 {
105
106         cyg_uint32 retcode = FLASH_ERR_OK;
107         void *err_addr;
108         cyg_uint32 addr = (cyg_uint32)flash_addr;
109         cyg_uint32 offset;
110
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;
116         }
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));
123                         return;
124                 }
125
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));
131                         return;
132                 }
133                 current_flash_page = flash_block_begin(addr);
134                 flash_read(flash_buffer, current_flash_page, flash_block_size, &err_addr);
135         }
136
137         offset = flash_addr - current_flash_page;
138         CYG_ASSERT(offset < flash_block_size, "offset not inside flash block");
139
140         flash_buffer[offset] = value;
141 }
142
143 // Program the current page into flash.
144 void flash_load_finish(void)
145 {
146         cyg_uint32 retcode = FLASH_ERR_OK;
147         void *err_addr;
148
149         if (init_done && flash_page_init) {
150                 flash_page_init = false;
151
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));
155                 } else {
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));
161                         }
162                 }
163         }
164         flash_init(diag_printf);
165 }
166
167 // This is called during redboot start up. We allocate a buffer the
168 // size of the flash page.
169 void
170 flash_load_init(void)
171 {
172         int flash_blocks;
173
174         flash_get_block_info(&flash_block_size, &flash_blocks);
175         workspace_end -= flash_block_size;
176
177         flash_buffer = workspace_end;
178 }
179
180 // Register this initialization function in the table
181 RedBoot_init(flash_load_init, RedBoot_INIT_LAST);