1 /***************************************************************************
2 * Copyright (C) 2009 Zachary T Welch <zw@superlucidity.net> *
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. *
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. *
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 ***************************************************************************/
23 #include <flash/flash.h>
24 #include <flash/nor/imp.h>
25 #include <target/image.h>
27 // in flash.c, to be moved here
28 extern struct flash_bank *flash_banks;
30 int flash_driver_erase(struct flash_bank *bank, int first, int last)
34 retval = bank->driver->erase(bank, first, last);
35 if (retval != ERROR_OK)
37 LOG_ERROR("failed erasing sectors %d to %d (%d)", first, last, retval);
43 int flash_driver_protect(struct flash_bank *bank, int set, int first, int last)
47 retval = bank->driver->protect(bank, set, first, last);
48 if (retval != ERROR_OK)
50 LOG_ERROR("failed setting protection for areas %d to %d (%d)", first, last, retval);
56 int flash_driver_write(struct flash_bank *bank,
57 uint8_t *buffer, uint32_t offset, uint32_t count)
61 retval = bank->driver->write(bank, buffer, offset, count);
62 if (retval != ERROR_OK)
64 LOG_ERROR("error writing to flash at address 0x%08" PRIx32 " at offset 0x%8.8" PRIx32 " (%d)",
65 bank->base, offset, retval);
72 void flash_bank_add(struct flash_bank *bank)
74 /* put flash bank in linked list */
75 unsigned bank_num = 0;
78 /* find last flash bank */
79 struct flash_bank *p = flash_banks;
80 while (NULL != p->next)
91 bank->bank_number = bank_num;
94 struct flash_bank *flash_bank_list(void)
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))
103 struct flash_bank *c;
108 if ((c = get_flash_bank_by_addr(target, addr)) == NULL)
109 return ERROR_FLASH_DST_OUT_OF_BANK; /* no corresponding bank found */
111 if (c->size == 0 || c->num_sectors == 0)
113 LOG_ERROR("Bank is invalid");
114 return ERROR_FLASH_BANK_INVALID;
119 /* special case, erase whole bank when length is zero */
121 return ERROR_FLASH_DST_BREAKS_ALIGNMENT;
123 return callback(c, 0, c->num_sectors - 1);
126 /* check whether it fits */
127 if (addr + length - 1 > c->base + c->size - 1)
128 return ERROR_FLASH_DST_BREAKS_ALIGNMENT;
132 for (i = 0; i < c->num_sectors; i++)
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 */
139 last = i; /* and it is the last one so far in any case */
143 if (first == -1 || last == -1)
146 return callback(c, first, last);
149 int flash_erase_address_range(struct target *target, uint32_t addr, uint32_t length)
151 return flash_iterate_address_range(target,
152 addr, length, &flash_driver_erase);
155 static int flash_driver_unprotect(struct flash_bank *bank, int first, int last)
157 return flash_driver_protect(bank, 0, first, last);
160 static int flash_unlock_address_range(struct target *target, uint32_t addr, uint32_t length)
162 return flash_iterate_address_range(target,
163 addr, length, &flash_driver_unprotect);
166 int flash_write_unlock(struct target *target, struct image *image,
167 uint32_t *written, int erase, bool unlock)
169 int retval = ERROR_OK;
172 uint32_t section_offset;
173 struct flash_bank *c;
184 /* assume all sectors need erasing - stops any problems
185 * when flash_write is called multiple times */
190 /* allocate padding array */
191 padding = malloc(image->num_sections * sizeof(padding));
193 /* loop until we reach end of the image */
194 while (section < image->num_sections)
196 uint32_t buffer_size;
200 uint32_t run_address = image->sections[section].base_address + section_offset;
201 uint32_t run_size = image->sections[section].size - section_offset;
204 if (image->sections[section].size == 0)
206 LOG_WARNING("empty section %d", section);
212 /* find the corresponding flash bank */
213 if ((c = get_flash_bank_by_addr(target, run_address)) == NULL)
215 section++; /* and skip it */
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))
227 if (image->sections[section_last + 1].base_address < (run_address + run_size))
229 LOG_DEBUG("section %d out of order(very slightly surprising, but supported)", section_last + 1);
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))
237 padding[section_last] = pad_bytes;
238 run_size += image->sections[++section_last].size;
239 run_size += pad_bytes;
240 padding[section_last] = 0;
242 LOG_INFO("Padding image section %d with %d bytes", section_last-1, pad_bytes);
245 /* fit the run into bank constraints */
246 if (run_address + run_size - 1 > c->base + c->size - 1)
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;
253 /* allocate buffer */
254 buffer = malloc(run_size);
257 /* read sections to the buffer */
258 while (buffer_size < run_size)
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;
266 if ((retval = image_read_section(image, section, section_offset,
267 size_read, buffer + buffer_size, &size_read)) != ERROR_OK || size_read == 0)
274 /* see if we need to pad the section */
275 while (padding[section]--)
276 (buffer + buffer_size)[size_read++] = 0xff;
278 buffer_size += size_read;
279 section_offset += size_read;
281 if (section_offset >= image->sections[section].size)
292 retval = flash_unlock_address_range(target, run_address, run_size);
294 if (retval == ERROR_OK)
298 /* calculate and erase sectors */
299 retval = flash_erase_address_range(target, run_address, run_size);
303 if (retval == ERROR_OK)
305 /* write flash sectors */
306 retval = flash_driver_write(c, buffer, run_address - c->base, run_size);
311 if (retval != ERROR_OK)
314 return retval; /* abort operation */
318 *written += run_size; /* add run size to total written counter */
326 int flash_write(struct target *target, struct image *image,
327 uint32_t *written, int erase)
329 return flash_write_unlock(target, image, written, erase, false);