1 /***************************************************************************
2 * Copyright (C) 2005 by Dominic Rath <Dominic.Rath@gmx.de> *
3 * Copyright (C) 2007,2008 Øyvind Harboe <oyvind.harboe@zylin.com> *
4 * Copyright (C) 2008 by Spencer Oliver <spen@spen-soft.co.uk> *
5 * Copyright (C) 2009 Zachary T Welch <zw@superlucidity.net> *
7 * This program is free software; you can redistribute it and/or modify *
8 * it under the terms of the GNU General Public License as published by *
9 * the Free Software Foundation; either version 2 of the License, or *
10 * (at your option) any later version. *
12 * This program is distributed in the hope that it will be useful, *
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15 * GNU General Public License for more details. *
17 * You should have received a copy of the GNU General Public License *
18 * along with this program; if not, write to the *
19 * Free Software Foundation, Inc., *
20 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
21 ***************************************************************************/
26 #include <flash/common.h>
27 #include <flash/nor/core.h>
28 #include <flash/nor/imp.h>
29 #include <target/image.h>
32 struct object
*flash_banks
= NULL
;
34 int flash_driver_erase(struct flash_bank
*bank
, int first
, int last
)
38 retval
= flash_bank_driver(bank
)->erase(bank
, first
, last
);
39 if (retval
!= ERROR_OK
)
41 LOG_ERROR("failed erasing sectors %d to %d (%d)", first
, last
, retval
);
47 int flash_driver_protect(struct flash_bank
*bank
, int set
, int first
, int last
)
51 retval
= flash_bank_driver(bank
)->protect(bank
, set
, first
, last
);
52 if (retval
!= ERROR_OK
)
54 LOG_ERROR("failed setting protection for areas %d to %d (%d)", first
, last
, retval
);
60 int flash_driver_write(struct flash_bank
*bank
,
61 uint8_t *buffer
, uint32_t offset
, uint32_t count
)
65 retval
= flash_bank_driver(bank
)->write(bank
, buffer
, offset
, count
);
66 if (retval
!= ERROR_OK
)
68 LOG_ERROR("error writing to flash at address 0x%08" PRIx32
" at offset 0x%8.8" PRIx32
" (%d)",
69 bank
->base
, offset
, retval
);
75 void flash_bank_add(struct flash_bank
*bank
)
77 object_add(&flash_banks
, &bank
->object
);
79 struct flash_bank
*flash_bank_list(void)
81 return flash_banks
? flash_bank_from_object(flash_banks
) : NULL
;
84 struct flash_bank
*get_flash_bank_by_num_noprobe(int num
)
89 for (p
= flash_bank_list(); NULL
!= p
; p
= flash_bank_next(p
))
96 LOG_ERROR("flash bank %d does not exist", num
);
100 int flash_get_bank_count(void)
102 struct flash_bank
*p
;
104 for (p
= flash_bank_list(); NULL
!= p
; p
= flash_bank_next(p
))
111 struct flash_bank
*get_flash_bank_by_name(const char *name
)
113 unsigned requested
= get_flash_name_index(name
);
116 struct flash_bank
*bank
;
117 for (bank
= flash_bank_list(); bank
; bank
= flash_bank_next(bank
))
119 if (strcmp(flash_bank_name(bank
), name
) == 0)
121 const char *dname
= flash_driver_name(flash_bank_driver(bank
));
122 if (!flash_driver_name_matches(dname
, name
))
124 if (++found
< requested
)
131 struct flash_bank
*get_flash_bank_by_num(int num
)
133 struct flash_bank
*p
= get_flash_bank_by_num_noprobe(num
);
139 retval
= flash_bank_driver(p
)->auto_probe(p
);
141 if (retval
!= ERROR_OK
)
143 LOG_ERROR("auto_probe failed %d\n", retval
);
149 /* lookup flash bank by address */
150 struct flash_bank
*get_flash_bank_by_addr(struct target
*target
, uint32_t addr
)
152 struct flash_bank
*c
;
154 /* cycle through bank list */
155 for (c
= flash_bank_list(); NULL
!= c
; c
= flash_bank_next(c
))
158 retval
= flash_bank_driver(c
)->auto_probe(c
);
160 if (retval
!= ERROR_OK
)
162 LOG_ERROR("auto_probe failed %d\n", retval
);
165 /* check whether address belongs to this flash bank */
166 if ((addr
>= c
->base
) && (addr
<= c
->base
+ (c
->size
- 1)) && target
== c
->target
)
169 LOG_ERROR("No flash at address 0x%08" PRIx32
"\n", addr
);
173 int default_flash_mem_blank_check(struct flash_bank
*bank
)
175 struct target
*target
= bank
->target
;
176 const int buffer_size
= 1024;
179 int retval
= ERROR_OK
;
181 if (bank
->target
->state
!= TARGET_HALTED
)
183 LOG_ERROR("Target not halted");
184 return ERROR_TARGET_NOT_HALTED
;
187 uint8_t *buffer
= malloc(buffer_size
);
189 for (i
= 0; i
< bank
->num_sectors
; i
++)
192 bank
->sectors
[i
].is_erased
= 1;
194 for (j
= 0; j
< bank
->sectors
[i
].size
; j
+= buffer_size
)
198 if (chunk
> (j
- bank
->sectors
[i
].size
))
200 chunk
= (j
- bank
->sectors
[i
].size
);
203 retval
= target_read_memory(target
, bank
->base
+ bank
->sectors
[i
].offset
+ j
, 4, chunk
/4, buffer
);
204 if (retval
!= ERROR_OK
)
209 for (nBytes
= 0; nBytes
< chunk
; nBytes
++)
211 if (buffer
[nBytes
] != 0xFF)
213 bank
->sectors
[i
].is_erased
= 0;
226 int default_flash_blank_check(struct flash_bank
*bank
)
228 struct target
*target
= bank
->target
;
234 if (bank
->target
->state
!= TARGET_HALTED
)
236 LOG_ERROR("Target not halted");
237 return ERROR_TARGET_NOT_HALTED
;
240 for (i
= 0; i
< bank
->num_sectors
; i
++)
242 uint32_t address
= bank
->base
+ bank
->sectors
[i
].offset
;
243 uint32_t size
= bank
->sectors
[i
].size
;
245 if ((retval
= target_blank_check_memory(target
, address
, size
, &blank
)) != ERROR_OK
)
251 bank
->sectors
[i
].is_erased
= 1;
253 bank
->sectors
[i
].is_erased
= 0;
259 LOG_USER("Running slow fallback erase check - add working memory");
260 return default_flash_mem_blank_check(bank
);
265 /* erase given flash region, selects proper bank according to target and address */
266 static int flash_iterate_address_range(struct target
*target
, uint32_t addr
, uint32_t length
,
267 int (*callback
)(struct flash_bank
*bank
, int first
, int last
))
269 struct flash_bank
*c
;
274 if ((c
= get_flash_bank_by_addr(target
, addr
)) == NULL
)
275 return ERROR_FLASH_DST_OUT_OF_BANK
; /* no corresponding bank found */
277 if (c
->size
== 0 || c
->num_sectors
== 0)
279 LOG_ERROR("Bank is invalid");
280 return ERROR_FLASH_BANK_INVALID
;
285 /* special case, erase whole bank when length is zero */
287 return ERROR_FLASH_DST_BREAKS_ALIGNMENT
;
289 return callback(c
, 0, c
->num_sectors
- 1);
292 /* check whether it fits */
293 if (addr
+ length
- 1 > c
->base
+ c
->size
- 1)
294 return ERROR_FLASH_DST_BREAKS_ALIGNMENT
;
298 for (i
= 0; i
< c
->num_sectors
; i
++)
300 /* check whether sector overlaps with the given range and is not yet erased */
301 if (addr
< c
->sectors
[i
].offset
+ c
->sectors
[i
].size
&& addr
+ length
> c
->sectors
[i
].offset
&& c
->sectors
[i
].is_erased
!= 1) {
302 /* if first is not set yet then this is the first sector */
305 last
= i
; /* and it is the last one so far in any case */
309 if (first
== -1 || last
== -1)
312 return callback(c
, first
, last
);
315 int flash_erase_address_range(struct target
*target
, uint32_t addr
, uint32_t length
)
317 return flash_iterate_address_range(target
,
318 addr
, length
, &flash_driver_erase
);
321 static int flash_driver_unprotect(struct flash_bank
*bank
, int first
, int last
)
323 return flash_driver_protect(bank
, 0, first
, last
);
326 static int flash_unlock_address_range(struct target
*target
, uint32_t addr
, uint32_t length
)
328 return flash_iterate_address_range(target
,
329 addr
, length
, &flash_driver_unprotect
);
332 int flash_write_unlock(struct target
*target
, struct image
*image
,
333 uint32_t *written
, int erase
, bool unlock
)
335 int retval
= ERROR_OK
;
338 uint32_t section_offset
;
339 struct flash_bank
*c
;
350 /* assume all sectors need erasing - stops any problems
351 * when flash_write is called multiple times */
356 /* allocate padding array */
357 padding
= malloc(image
->num_sections
* sizeof(padding
));
359 /* loop until we reach end of the image */
360 while (section
< image
->num_sections
)
362 uint32_t buffer_size
;
366 uint32_t run_address
= image
->sections
[section
].base_address
+ section_offset
;
367 uint32_t run_size
= image
->sections
[section
].size
- section_offset
;
370 if (image
->sections
[section
].size
== 0)
372 LOG_WARNING("empty section %d", section
);
378 /* find the corresponding flash bank */
379 if ((c
= get_flash_bank_by_addr(target
, run_address
)) == NULL
)
381 section
++; /* and skip it */
386 /* collect consecutive sections which fall into the same bank */
387 section_first
= section
;
388 section_last
= section
;
389 padding
[section
] = 0;
390 while ((run_address
+ run_size
- 1 < c
->base
+ c
->size
- 1)
391 && (section_last
+ 1 < image
->num_sections
))
393 if (image
->sections
[section_last
+ 1].base_address
< (run_address
+ run_size
))
395 LOG_DEBUG("section %d out of order(very slightly surprising, but supported)", section_last
+ 1);
398 /* if we have multiple sections within our image, flash programming could fail due to alignment issues
399 * attempt to rebuild a consecutive buffer for the flash loader */
400 pad_bytes
= (image
->sections
[section_last
+ 1].base_address
) - (run_address
+ run_size
);
401 if ((run_address
+ run_size
+ pad_bytes
) > (c
->base
+ c
->size
))
403 padding
[section_last
] = pad_bytes
;
404 run_size
+= image
->sections
[++section_last
].size
;
405 run_size
+= pad_bytes
;
406 padding
[section_last
] = 0;
408 LOG_INFO("Padding image section %d with %d bytes", section_last
-1, pad_bytes
);
411 /* fit the run into bank constraints */
412 if (run_address
+ run_size
- 1 > c
->base
+ c
->size
- 1)
414 LOG_WARNING("writing %d bytes only - as image section is %d bytes and bank is only %d bytes", \
415 (int)(c
->base
+ c
->size
- run_address
), (int)(run_size
), (int)(c
->size
));
416 run_size
= c
->base
+ c
->size
- run_address
;
419 /* allocate buffer */
420 buffer
= malloc(run_size
);
423 /* read sections to the buffer */
424 while (buffer_size
< run_size
)
428 size_read
= run_size
- buffer_size
;
429 if (size_read
> image
->sections
[section
].size
- section_offset
)
430 size_read
= image
->sections
[section
].size
- section_offset
;
432 if ((retval
= image_read_section(image
, section
, section_offset
,
433 size_read
, buffer
+ buffer_size
, &size_read
)) != ERROR_OK
|| size_read
== 0)
440 /* see if we need to pad the section */
441 while (padding
[section
]--)
442 (buffer
+ buffer_size
)[size_read
++] = 0xff;
444 buffer_size
+= size_read
;
445 section_offset
+= size_read
;
447 if (section_offset
>= image
->sections
[section
].size
)
458 retval
= flash_unlock_address_range(target
, run_address
, run_size
);
460 if (retval
== ERROR_OK
)
464 /* calculate and erase sectors */
465 retval
= flash_erase_address_range(target
, run_address
, run_size
);
469 if (retval
== ERROR_OK
)
471 /* write flash sectors */
472 retval
= flash_driver_write(c
, buffer
, run_address
- c
->base
, run_size
);
477 if (retval
!= ERROR_OK
)
480 return retval
; /* abort operation */
484 *written
+= run_size
; /* add run size to total written counter */
492 int flash_write(struct target
*target
, struct image
*image
,
493 uint32_t *written
, int erase
)
495 return flash_write_unlock(target
, image
, written
, erase
, false);