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