]> git.kernelconcepts.de Git - karo-tx-redboot.git/blob - packages/redboot/v2_0/src/flash_load.c
Initial revision
[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   return 0;
78 }
79
80 // Calculate the address of the first byte in a flash block
81 static cyg_uint8 * flash_block_begin(cyg_uint32 addr) 
82 {
83   return (cyg_uint8 *)
84     ((addr / flash_block_size) * flash_block_size);
85 }
86
87 // Initialize the loading process
88 void flash_load_start(void)
89 {
90   flash_init(dummy_printf);
91   
92   init_done = true;
93   flash_page_init = false;
94 }
95
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)
103 {
104   
105   cyg_uint32 retcode = FLASH_ERR_OK;
106   void * err_addr;
107   cyg_uint32 addr = (cyg_uint32)flash_addr;
108   cyg_uint32 offset;
109   
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;
115   } 
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));
122       return;
123     }
124     
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));
130       return;
131     }
132     current_flash_page = flash_block_begin(addr);
133     flash_read(flash_buffer, current_flash_page, flash_block_size, &err_addr);
134   }
135   
136   offset = flash_addr - current_flash_page;
137   CYG_ASSERT(offset < flash_block_size, "offset not inside flash block");
138   
139   flash_buffer[offset] = value;
140 }
141
142 // Program the current page into flash.
143 void flash_load_finish(void)
144 {
145   cyg_uint32 retcode = FLASH_ERR_OK;
146   void * err_addr;
147   
148   if (init_done && flash_page_init) {
149     flash_page_init = false;
150     
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));
154     } else {
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));
160       }
161     }
162   }
163   flash_init(diag_printf);
164 }
165
166 // This is called during redboot start up. We allocate a buffer the
167 // size of the flash page.
168 void
169 flash_load_init(void)
170 {
171   int flash_blocks;
172
173   flash_get_block_info(&flash_block_size, &flash_blocks);
174   workspace_end -= flash_block_size;
175   
176   flash_buffer = workspace_end;
177 }
178
179 // Register this initialization function in the table
180 RedBoot_init(flash_load_init, RedBoot_INIT_LAST);
181
182
183