add flash/nor/drivers.c
[fw/openocd] / src / flash / nor / core.c
1 /***************************************************************************
2  *   Copyright (C) 2009 Zachary T Welch <zw@superlucidity.net>             *
3  *                                                                         *
4  *   This program is free software; you can redistribute it and/or modify  *
5  *   it under the terms of the GNU General Public License as published by  *
6  *   the Free Software Foundation; either version 2 of the License, or     *
7  *   (at your option) any later version.                                   *
8  *                                                                         *
9  *   This program is distributed in the hope that it will be useful,       *
10  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
11  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
12  *   GNU General Public License for more details.                          *
13  *                                                                         *
14  *   You should have received a copy of the GNU General Public License     *
15  *   along with this program; if not, write to the                         *
16  *   Free Software Foundation, Inc.,                                       *
17  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
18  ***************************************************************************/
19
20 #ifdef HAVE_CONFIG_H
21 #include <config.h>
22 #endif
23 #include <flash/flash.h>
24 #include <flash/nor/imp.h>
25 #include <target/image.h>
26
27 // in flash.c, to be moved here
28 extern struct flash_bank *flash_banks;
29
30 int flash_driver_erase(struct flash_bank *bank, int first, int last)
31 {
32         int retval;
33
34         retval = bank->driver->erase(bank, first, last);
35         if (retval != ERROR_OK)
36         {
37                 LOG_ERROR("failed erasing sectors %d to %d (%d)", first, last, retval);
38         }
39
40         return retval;
41 }
42
43 int flash_driver_protect(struct flash_bank *bank, int set, int first, int last)
44 {
45         int retval;
46
47         retval = bank->driver->protect(bank, set, first, last);
48         if (retval != ERROR_OK)
49         {
50                 LOG_ERROR("failed setting protection for areas %d to %d (%d)", first, last, retval);
51         }
52
53         return retval;
54 }
55
56 int flash_driver_write(struct flash_bank *bank,
57                 uint8_t *buffer, uint32_t offset, uint32_t count)
58 {
59         int retval;
60
61         retval = bank->driver->write(bank, buffer, offset, count);
62         if (retval != ERROR_OK)
63         {
64                 LOG_ERROR("error writing to flash at address 0x%08" PRIx32 " at offset 0x%8.8" PRIx32 " (%d)",
65                           bank->base, offset, retval);
66         }
67
68         return retval;
69 }
70
71
72 void flash_bank_add(struct flash_bank *bank)
73 {
74         /* put flash bank in linked list */
75         unsigned bank_num = 0;
76         if (flash_banks)
77         {
78                 /* find last flash bank */
79                 struct flash_bank *p = flash_banks;
80                 while (NULL != p->next)
81                 {
82                         bank_num += 1;
83                         p = p->next;
84                 }
85                 p->next = bank;
86                 bank_num += 1;
87         }
88         else
89                 flash_banks = bank;
90
91         bank->bank_number = bank_num;
92 }
93
94 struct flash_bank *flash_bank_list(void)
95 {
96         return flash_banks;
97 }
98
99 /* erase given flash region, selects proper bank according to target and address */
100 static int flash_iterate_address_range(struct target *target, uint32_t addr, uint32_t length,
101                 int (*callback)(struct flash_bank *bank, int first, int last))
102 {
103         struct flash_bank *c;
104         int first = -1;
105         int last = -1;
106         int i;
107
108         if ((c = get_flash_bank_by_addr(target, addr)) == NULL)
109                 return ERROR_FLASH_DST_OUT_OF_BANK; /* no corresponding bank found */
110
111         if (c->size == 0 || c->num_sectors == 0)
112         {
113                 LOG_ERROR("Bank is invalid");
114                 return ERROR_FLASH_BANK_INVALID;
115         }
116
117         if (length == 0)
118         {
119                 /* special case, erase whole bank when length is zero */
120                 if (addr != c->base)
121                         return ERROR_FLASH_DST_BREAKS_ALIGNMENT;
122
123                 return callback(c, 0, c->num_sectors - 1);
124         }
125
126         /* check whether it fits */
127         if (addr + length - 1 > c->base + c->size - 1)
128                 return ERROR_FLASH_DST_BREAKS_ALIGNMENT;
129
130         addr -= c->base;
131
132         for (i = 0; i < c->num_sectors; i++)
133         {
134                 /* check whether sector overlaps with the given range and is not yet erased */
135                 if (addr < c->sectors[i].offset + c->sectors[i].size && addr + length > c->sectors[i].offset && c->sectors[i].is_erased != 1) {
136                         /* if first is not set yet then this is the first sector */
137                         if (first == -1)
138                                 first = i;
139                         last = i; /* and it is the last one so far in any case */
140                 }
141         }
142
143         if (first == -1 || last == -1)
144                 return ERROR_OK;
145
146         return callback(c, first, last);
147 }
148
149 int flash_erase_address_range(struct target *target, uint32_t addr, uint32_t length)
150 {
151         return flash_iterate_address_range(target,
152                         addr, length, &flash_driver_erase);
153 }
154
155 static int flash_driver_unprotect(struct flash_bank *bank, int first, int last)
156 {
157         return flash_driver_protect(bank, 0, first, last);
158 }
159
160 static int flash_unlock_address_range(struct target *target, uint32_t addr, uint32_t length)
161 {
162         return flash_iterate_address_range(target,
163                         addr, length, &flash_driver_unprotect);
164 }
165
166 int flash_write_unlock(struct target *target, struct image *image,
167                 uint32_t *written, int erase, bool unlock)
168 {
169         int retval = ERROR_OK;
170
171         int section;
172         uint32_t section_offset;
173         struct flash_bank *c;
174         int *padding;
175
176         section = 0;
177         section_offset = 0;
178
179         if (written)
180                 *written = 0;
181
182         if (erase)
183         {
184                 /* assume all sectors need erasing - stops any problems
185                  * when flash_write is called multiple times */
186
187                 flash_set_dirty();
188         }
189
190         /* allocate padding array */
191         padding = malloc(image->num_sections * sizeof(padding));
192
193         /* loop until we reach end of the image */
194         while (section < image->num_sections)
195         {
196                 uint32_t buffer_size;
197                 uint8_t *buffer;
198                 int section_first;
199                 int section_last;
200                 uint32_t run_address = image->sections[section].base_address + section_offset;
201                 uint32_t run_size = image->sections[section].size - section_offset;
202                 int pad_bytes = 0;
203
204                 if (image->sections[section].size ==  0)
205                 {
206                         LOG_WARNING("empty section %d", section);
207                         section++;
208                         section_offset = 0;
209                         continue;
210                 }
211
212                 /* find the corresponding flash bank */
213                 if ((c = get_flash_bank_by_addr(target, run_address)) == NULL)
214                 {
215                         section++; /* and skip it */
216                         section_offset = 0;
217                         continue;
218                 }
219
220                 /* collect consecutive sections which fall into the same bank */
221                 section_first = section;
222                 section_last = section;
223                 padding[section] = 0;
224                 while ((run_address + run_size - 1 < c->base + c->size - 1)
225                                 && (section_last + 1 < image->num_sections))
226                 {
227                         if (image->sections[section_last + 1].base_address < (run_address + run_size))
228                         {
229                                 LOG_DEBUG("section %d out of order(very slightly surprising, but supported)", section_last + 1);
230                                 break;
231                         }
232                         /* if we have multiple sections within our image, flash programming could fail due to alignment issues
233                          * attempt to rebuild a consecutive buffer for the flash loader */
234                         pad_bytes = (image->sections[section_last + 1].base_address) - (run_address + run_size);
235                         if ((run_address + run_size + pad_bytes) > (c->base + c->size))
236                                 break;
237                         padding[section_last] = pad_bytes;
238                         run_size += image->sections[++section_last].size;
239                         run_size += pad_bytes;
240                         padding[section_last] = 0;
241
242                         LOG_INFO("Padding image section %d with %d bytes", section_last-1, pad_bytes);
243                 }
244
245                 /* fit the run into bank constraints */
246                 if (run_address + run_size - 1 > c->base + c->size - 1)
247                 {
248                         LOG_WARNING("writing %d bytes only - as image section is %d bytes and bank is only %d bytes", \
249                                     (int)(c->base + c->size - run_address), (int)(run_size), (int)(c->size));
250                         run_size = c->base + c->size - run_address;
251                 }
252
253                 /* allocate buffer */
254                 buffer = malloc(run_size);
255                 buffer_size = 0;
256
257                 /* read sections to the buffer */
258                 while (buffer_size < run_size)
259                 {
260                         size_t size_read;
261
262                         size_read = run_size - buffer_size;
263                         if (size_read > image->sections[section].size - section_offset)
264                             size_read = image->sections[section].size - section_offset;
265
266                         if ((retval = image_read_section(image, section, section_offset,
267                                         size_read, buffer + buffer_size, &size_read)) != ERROR_OK || size_read == 0)
268                         {
269                                 free(buffer);
270                                 free(padding);
271                                 return retval;
272                         }
273
274                         /* see if we need to pad the section */
275                         while (padding[section]--)
276                                  (buffer + buffer_size)[size_read++] = 0xff;
277
278                         buffer_size += size_read;
279                         section_offset += size_read;
280
281                         if (section_offset >= image->sections[section].size)
282                         {
283                                 section++;
284                                 section_offset = 0;
285                         }
286                 }
287
288                 retval = ERROR_OK;
289
290                 if (unlock)
291                 {
292                         retval = flash_unlock_address_range(target, run_address, run_size);
293                 }
294                 if (retval == ERROR_OK)
295                 {
296                         if (erase)
297                         {
298                                 /* calculate and erase sectors */
299                                 retval = flash_erase_address_range(target, run_address, run_size);
300                         }
301                 }
302
303                 if (retval == ERROR_OK)
304                 {
305                         /* write flash sectors */
306                         retval = flash_driver_write(c, buffer, run_address - c->base, run_size);
307                 }
308
309                 free(buffer);
310
311                 if (retval != ERROR_OK)
312                 {
313                         free(padding);
314                         return retval; /* abort operation */
315                 }
316
317                 if (written != NULL)
318                         *written += run_size; /* add run size to total written counter */
319         }
320
321         free(padding);
322
323         return retval;
324 }
325
326 int flash_write(struct target *target, struct image *image,
327                 uint32_t *written, int erase)
328 {
329         return flash_write_unlock(target, image, written, erase, false);
330 }