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_driver *flash_drivers[];
29 extern struct flash_bank *flash_banks;
31 struct flash_driver *flash_driver_find_by_name(const char *name)
33 for (unsigned i = 0; flash_drivers[i]; i++)
35 if (strcmp(name, flash_drivers[i]->name) == 0)
36 return flash_drivers[i];
41 int flash_driver_erase(struct flash_bank *bank, int first, int last)
45 retval = bank->driver->erase(bank, first, last);
46 if (retval != ERROR_OK)
48 LOG_ERROR("failed erasing sectors %d to %d (%d)", first, last, retval);
54 int flash_driver_protect(struct flash_bank *bank, int set, int first, int last)
58 retval = bank->driver->protect(bank, set, first, last);
59 if (retval != ERROR_OK)
61 LOG_ERROR("failed setting protection for areas %d to %d (%d)", first, last, retval);
67 int flash_driver_write(struct flash_bank *bank,
68 uint8_t *buffer, uint32_t offset, uint32_t count)
72 retval = bank->driver->write(bank, buffer, offset, count);
73 if (retval != ERROR_OK)
75 LOG_ERROR("error writing to flash at address 0x%08" PRIx32 " at offset 0x%8.8" PRIx32 " (%d)",
76 bank->base, offset, retval);
83 void flash_bank_add(struct flash_bank *bank)
85 /* put flash bank in linked list */
86 unsigned bank_num = 0;
89 /* find last flash bank */
90 struct flash_bank *p = flash_banks;
91 while (NULL != p->next)
102 bank->bank_number = bank_num;
105 struct flash_bank *flash_bank_list(void)
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))
114 struct flash_bank *c;
119 if ((c = get_flash_bank_by_addr(target, addr)) == NULL)
120 return ERROR_FLASH_DST_OUT_OF_BANK; /* no corresponding bank found */
122 if (c->size == 0 || c->num_sectors == 0)
124 LOG_ERROR("Bank is invalid");
125 return ERROR_FLASH_BANK_INVALID;
130 /* special case, erase whole bank when length is zero */
132 return ERROR_FLASH_DST_BREAKS_ALIGNMENT;
134 return callback(c, 0, c->num_sectors - 1);
137 /* check whether it fits */
138 if (addr + length - 1 > c->base + c->size - 1)
139 return ERROR_FLASH_DST_BREAKS_ALIGNMENT;
143 for (i = 0; i < c->num_sectors; i++)
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 */
150 last = i; /* and it is the last one so far in any case */
154 if (first == -1 || last == -1)
157 return callback(c, first, last);
160 int flash_erase_address_range(struct target *target, uint32_t addr, uint32_t length)
162 return flash_iterate_address_range(target,
163 addr, length, &flash_driver_erase);
166 static int flash_driver_unprotect(struct flash_bank *bank, int first, int last)
168 return flash_driver_protect(bank, 0, first, last);
171 static int flash_unlock_address_range(struct target *target, uint32_t addr, uint32_t length)
173 return flash_iterate_address_range(target,
174 addr, length, &flash_driver_unprotect);
177 int flash_write_unlock(struct target *target, struct image *image,
178 uint32_t *written, int erase, bool unlock)
180 int retval = ERROR_OK;
183 uint32_t section_offset;
184 struct flash_bank *c;
195 /* assume all sectors need erasing - stops any problems
196 * when flash_write is called multiple times */
201 /* allocate padding array */
202 padding = malloc(image->num_sections * sizeof(padding));
204 /* loop until we reach end of the image */
205 while (section < image->num_sections)
207 uint32_t buffer_size;
211 uint32_t run_address = image->sections[section].base_address + section_offset;
212 uint32_t run_size = image->sections[section].size - section_offset;
215 if (image->sections[section].size == 0)
217 LOG_WARNING("empty section %d", section);
223 /* find the corresponding flash bank */
224 if ((c = get_flash_bank_by_addr(target, run_address)) == NULL)
226 section++; /* and skip it */
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))
238 if (image->sections[section_last + 1].base_address < (run_address + run_size))
240 LOG_DEBUG("section %d out of order(very slightly surprising, but supported)", section_last + 1);
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))
248 padding[section_last] = pad_bytes;
249 run_size += image->sections[++section_last].size;
250 run_size += pad_bytes;
251 padding[section_last] = 0;
253 LOG_INFO("Padding image section %d with %d bytes", section_last-1, pad_bytes);
256 /* fit the run into bank constraints */
257 if (run_address + run_size - 1 > c->base + c->size - 1)
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;
264 /* allocate buffer */
265 buffer = malloc(run_size);
268 /* read sections to the buffer */
269 while (buffer_size < run_size)
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;
277 if ((retval = image_read_section(image, section, section_offset,
278 size_read, buffer + buffer_size, &size_read)) != ERROR_OK || size_read == 0)
285 /* see if we need to pad the section */
286 while (padding[section]--)
287 (buffer + buffer_size)[size_read++] = 0xff;
289 buffer_size += size_read;
290 section_offset += size_read;
292 if (section_offset >= image->sections[section].size)
303 retval = flash_unlock_address_range(target, run_address, run_size);
305 if (retval == ERROR_OK)
309 /* calculate and erase sectors */
310 retval = flash_erase_address_range(target, run_address, run_size);
314 if (retval == ERROR_OK)
316 /* write flash sectors */
317 retval = flash_driver_write(c, buffer, run_address - c->base, run_size);
322 if (retval != ERROR_OK)
325 return retval; /* abort operation */
329 *written += run_size; /* add run size to total written counter */
337 int flash_write(struct target *target, struct image *image,
338 uint32_t *written, int erase)
340 return flash_write_unlock(target, image, written, erase, false);