2009-12-03 19:14:07 -06:00
|
|
|
/***************************************************************************
|
2009-12-04 06:37:27 -06:00
|
|
|
* Copyright (C) 2005 by Dominic Rath <Dominic.Rath@gmx.de> *
|
2010-04-28 20:49:32 -05:00
|
|
|
* Copyright (C) 2007-2010 Øyvind Harboe <oyvind.harboe@zylin.com> *
|
2009-12-04 06:37:27 -06:00
|
|
|
* Copyright (C) 2008 by Spencer Oliver <spen@spen-soft.co.uk> *
|
2009-12-03 19:14:07 -06:00
|
|
|
* Copyright (C) 2009 Zachary T Welch <zw@superlucidity.net> *
|
2010-05-10 22:16:33 -05:00
|
|
|
* Copyright (C) 2010 by Antonio Borneo <borneo.antonio@gmail.com> *
|
2009-12-03 19:14:07 -06:00
|
|
|
* *
|
|
|
|
* This program is free software; you can redistribute it and/or modify *
|
|
|
|
* it under the terms of the GNU General Public License as published by *
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or *
|
|
|
|
* (at your option) any later version. *
|
|
|
|
* *
|
|
|
|
* This program is distributed in the hope that it will be useful, *
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
|
|
|
* GNU General Public License for more details. *
|
|
|
|
* *
|
|
|
|
* You should have received a copy of the GNU General Public License *
|
|
|
|
* along with this program; if not, write to the *
|
|
|
|
* Free Software Foundation, Inc., *
|
|
|
|
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
|
|
|
|
***************************************************************************/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include <config.h>
|
|
|
|
#endif
|
2009-12-04 06:37:27 -06:00
|
|
|
#include <flash/common.h>
|
|
|
|
#include <flash/nor/core.h>
|
2009-12-03 19:14:07 -06:00
|
|
|
#include <flash/nor/imp.h>
|
2009-12-04 06:01:45 -06:00
|
|
|
#include <target/image.h>
|
2009-12-03 19:14:07 -06:00
|
|
|
|
2009-12-04 06:37:27 -06:00
|
|
|
|
2010-01-11 02:14:01 -06:00
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
* Upper level of NOR flash framework.
|
|
|
|
* The lower level interfaces are to drivers. These upper level ones
|
|
|
|
* primarily support access from Tcl scripts or from GDB.
|
|
|
|
*/
|
|
|
|
|
2010-04-10 06:54:22 -05:00
|
|
|
static struct flash_bank *flash_banks;
|
2009-12-03 19:14:07 -06:00
|
|
|
|
2009-12-04 06:01:45 -06:00
|
|
|
int flash_driver_erase(struct flash_bank *bank, int first, int last)
|
|
|
|
{
|
|
|
|
int retval;
|
|
|
|
|
|
|
|
retval = bank->driver->erase(bank, first, last);
|
|
|
|
if (retval != ERROR_OK)
|
|
|
|
{
|
2011-01-03 06:30:28 -06:00
|
|
|
LOG_ERROR("failed erasing sectors %d to %d", first, last);
|
2009-12-04 06:01:45 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
int flash_driver_protect(struct flash_bank *bank, int set, int first, int last)
|
|
|
|
{
|
|
|
|
int retval;
|
2010-03-03 23:01:16 -06:00
|
|
|
|
|
|
|
/* callers may not supply illegal parameters ... */
|
|
|
|
if (first < 0 || first > last || last >= bank->num_sectors)
|
2010-05-05 08:08:34 -05:00
|
|
|
{
|
|
|
|
LOG_ERROR("illegal sector range");
|
2010-03-03 23:01:16 -06:00
|
|
|
return ERROR_FAIL;
|
2010-05-05 08:08:34 -05:00
|
|
|
}
|
2010-03-03 23:01:16 -06:00
|
|
|
|
|
|
|
/* force "set" to 0/1 */
|
|
|
|
set = !!set;
|
|
|
|
|
2010-05-05 08:08:34 -05:00
|
|
|
/* DANGER!
|
2010-03-03 23:01:16 -06:00
|
|
|
*
|
2010-05-05 08:08:34 -05:00
|
|
|
* We must not use any cached information about protection state!!!!
|
2010-04-15 21:48:55 -05:00
|
|
|
*
|
2010-05-05 08:08:34 -05:00
|
|
|
* There are a million things that could change the protect state:
|
|
|
|
*
|
|
|
|
* the target could have reset, power cycled, been hot plugged,
|
|
|
|
* the application could have run, etc.
|
|
|
|
*
|
|
|
|
* Drivers only receive valid sector range.
|
2010-04-15 21:48:55 -05:00
|
|
|
*/
|
2009-12-04 06:01:45 -06:00
|
|
|
retval = bank->driver->protect(bank, set, first, last);
|
|
|
|
if (retval != ERROR_OK)
|
|
|
|
{
|
2011-01-03 06:30:28 -06:00
|
|
|
LOG_ERROR("failed setting protection for areas %d to %d", first, last);
|
2009-12-04 06:01:45 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
int flash_driver_write(struct flash_bank *bank,
|
|
|
|
uint8_t *buffer, uint32_t offset, uint32_t count)
|
|
|
|
{
|
|
|
|
int retval;
|
|
|
|
|
|
|
|
retval = bank->driver->write(bank, buffer, offset, count);
|
|
|
|
if (retval != ERROR_OK)
|
|
|
|
{
|
2011-01-03 06:30:28 -06:00
|
|
|
LOG_ERROR("error writing to flash at address 0x%08" PRIx32 " at offset 0x%8.8" PRIx32,
|
|
|
|
bank->base, offset);
|
2009-12-04 06:01:45 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2010-05-10 22:16:33 -05:00
|
|
|
int flash_driver_read(struct flash_bank *bank,
|
|
|
|
uint8_t *buffer, uint32_t offset, uint32_t count)
|
|
|
|
{
|
|
|
|
int retval;
|
|
|
|
|
|
|
|
LOG_DEBUG("call flash_driver_read()");
|
|
|
|
|
|
|
|
retval = bank->driver->read(bank, buffer, offset, count);
|
|
|
|
if (retval != ERROR_OK)
|
|
|
|
{
|
2011-01-03 06:30:28 -06:00
|
|
|
LOG_ERROR("error reading to flash at address 0x%08" PRIx32 " at offset 0x%8.8" PRIx32,
|
|
|
|
bank->base, offset);
|
2010-05-10 22:16:33 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
int default_flash_read(struct flash_bank *bank,
|
|
|
|
uint8_t *buffer, uint32_t offset, uint32_t count)
|
|
|
|
{
|
|
|
|
return target_read_buffer(bank->target, offset + bank->base, count, buffer);
|
|
|
|
}
|
|
|
|
|
2009-12-03 19:14:07 -06:00
|
|
|
void flash_bank_add(struct flash_bank *bank)
|
|
|
|
{
|
|
|
|
/* put flash bank in linked list */
|
|
|
|
unsigned bank_num = 0;
|
|
|
|
if (flash_banks)
|
|
|
|
{
|
|
|
|
/* find last flash bank */
|
|
|
|
struct flash_bank *p = flash_banks;
|
|
|
|
while (NULL != p->next)
|
|
|
|
{
|
|
|
|
bank_num += 1;
|
|
|
|
p = p->next;
|
|
|
|
}
|
|
|
|
p->next = bank;
|
|
|
|
bank_num += 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
flash_banks = bank;
|
|
|
|
|
|
|
|
bank->bank_number = bank_num;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct flash_bank *flash_bank_list(void)
|
|
|
|
{
|
|
|
|
return flash_banks;
|
|
|
|
}
|
2009-12-04 06:01:45 -06:00
|
|
|
|
2009-12-04 06:37:27 -06:00
|
|
|
struct flash_bank *get_flash_bank_by_num_noprobe(int num)
|
|
|
|
{
|
|
|
|
struct flash_bank *p;
|
|
|
|
int i = 0;
|
|
|
|
|
|
|
|
for (p = flash_banks; p; p = p->next)
|
|
|
|
{
|
|
|
|
if (i++ == num)
|
|
|
|
{
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
LOG_ERROR("flash bank %d does not exist", num);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
int flash_get_bank_count(void)
|
|
|
|
{
|
|
|
|
struct flash_bank *p;
|
|
|
|
int i = 0;
|
|
|
|
for (p = flash_banks; p; p = p->next)
|
|
|
|
{
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
2010-05-24 06:30:29 -05:00
|
|
|
struct flash_bank *get_flash_bank_by_name_noprobe(const char *name)
|
2009-12-04 06:37:27 -06:00
|
|
|
{
|
|
|
|
unsigned requested = get_flash_name_index(name);
|
|
|
|
unsigned found = 0;
|
|
|
|
|
|
|
|
struct flash_bank *bank;
|
|
|
|
for (bank = flash_banks; NULL != bank; bank = bank->next)
|
|
|
|
{
|
|
|
|
if (strcmp(bank->name, name) == 0)
|
|
|
|
return bank;
|
|
|
|
if (!flash_driver_name_matches(bank->driver->name, name))
|
|
|
|
continue;
|
|
|
|
if (++found < requested)
|
|
|
|
continue;
|
|
|
|
return bank;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2010-06-11 01:10:39 -05:00
|
|
|
int get_flash_bank_by_name(const char *name, struct flash_bank **bank_result)
|
2010-05-24 06:30:29 -05:00
|
|
|
{
|
|
|
|
struct flash_bank *bank;
|
|
|
|
int retval;
|
|
|
|
|
|
|
|
bank = get_flash_bank_by_name_noprobe(name);
|
|
|
|
if (bank != NULL)
|
|
|
|
{
|
|
|
|
retval = bank->driver->auto_probe(bank);
|
|
|
|
|
|
|
|
if (retval != ERROR_OK)
|
|
|
|
{
|
2011-01-03 06:37:38 -06:00
|
|
|
LOG_ERROR("auto_probe failed");
|
2010-06-11 01:10:39 -05:00
|
|
|
return retval;
|
2010-05-24 06:30:29 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-06-11 01:10:39 -05:00
|
|
|
*bank_result = bank;
|
|
|
|
return ERROR_OK;
|
2010-05-24 06:30:29 -05:00
|
|
|
}
|
|
|
|
|
2010-05-04 06:26:52 -05:00
|
|
|
int get_flash_bank_by_num(int num, struct flash_bank **bank)
|
2009-12-04 06:37:27 -06:00
|
|
|
{
|
|
|
|
struct flash_bank *p = get_flash_bank_by_num_noprobe(num);
|
|
|
|
int retval;
|
|
|
|
|
|
|
|
if (p == NULL)
|
2010-05-04 06:26:52 -05:00
|
|
|
{
|
|
|
|
return ERROR_FAIL;
|
|
|
|
}
|
2009-12-04 06:37:27 -06:00
|
|
|
|
|
|
|
retval = p->driver->auto_probe(p);
|
|
|
|
|
|
|
|
if (retval != ERROR_OK)
|
|
|
|
{
|
2011-01-03 06:37:38 -06:00
|
|
|
LOG_ERROR("auto_probe failed");
|
2010-05-04 06:26:52 -05:00
|
|
|
return retval;
|
2009-12-04 06:37:27 -06:00
|
|
|
}
|
2010-05-04 06:26:52 -05:00
|
|
|
*bank = p;
|
|
|
|
return ERROR_OK;
|
2009-12-04 06:37:27 -06:00
|
|
|
}
|
|
|
|
|
2010-06-11 01:10:39 -05:00
|
|
|
/* lookup flash bank by address, bank not found is success, but
|
|
|
|
* result_bank is set to NULL. */
|
|
|
|
int get_flash_bank_by_addr(struct target *target, uint32_t addr, bool check, struct flash_bank **result_bank)
|
2009-12-04 06:37:27 -06:00
|
|
|
{
|
|
|
|
struct flash_bank *c;
|
|
|
|
|
|
|
|
/* cycle through bank list */
|
|
|
|
for (c = flash_banks; c; c = c->next)
|
|
|
|
{
|
|
|
|
int retval;
|
|
|
|
retval = c->driver->auto_probe(c);
|
|
|
|
|
|
|
|
if (retval != ERROR_OK)
|
|
|
|
{
|
2011-01-03 06:37:38 -06:00
|
|
|
LOG_ERROR("auto_probe failed");
|
2010-06-11 01:10:39 -05:00
|
|
|
return retval;
|
2009-12-04 06:37:27 -06:00
|
|
|
}
|
|
|
|
/* check whether address belongs to this flash bank */
|
|
|
|
if ((addr >= c->base) && (addr <= c->base + (c->size - 1)) && target == c->target)
|
2010-06-11 01:10:39 -05:00
|
|
|
{
|
|
|
|
*result_bank = c;
|
|
|
|
return ERROR_OK;
|
|
|
|
}
|
2009-12-04 06:37:27 -06:00
|
|
|
}
|
2010-06-11 01:10:39 -05:00
|
|
|
*result_bank = NULL;
|
|
|
|
if (check)
|
|
|
|
{
|
2011-01-03 06:37:38 -06:00
|
|
|
LOG_ERROR("No flash at address 0x%08" PRIx32, addr);
|
2010-06-11 01:10:39 -05:00
|
|
|
return ERROR_FAIL;
|
|
|
|
}
|
|
|
|
return ERROR_OK;
|
2009-12-04 06:37:27 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
int default_flash_mem_blank_check(struct flash_bank *bank)
|
|
|
|
{
|
|
|
|
struct target *target = bank->target;
|
|
|
|
const int buffer_size = 1024;
|
|
|
|
int i;
|
|
|
|
uint32_t nBytes;
|
|
|
|
int retval = ERROR_OK;
|
|
|
|
|
|
|
|
if (bank->target->state != TARGET_HALTED)
|
|
|
|
{
|
|
|
|
LOG_ERROR("Target not halted");
|
|
|
|
return ERROR_TARGET_NOT_HALTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t *buffer = malloc(buffer_size);
|
|
|
|
|
|
|
|
for (i = 0; i < bank->num_sectors; i++)
|
|
|
|
{
|
|
|
|
uint32_t j;
|
|
|
|
bank->sectors[i].is_erased = 1;
|
|
|
|
|
|
|
|
for (j = 0; j < bank->sectors[i].size; j += buffer_size)
|
|
|
|
{
|
|
|
|
uint32_t chunk;
|
|
|
|
chunk = buffer_size;
|
|
|
|
if (chunk > (j - bank->sectors[i].size))
|
|
|
|
{
|
|
|
|
chunk = (j - bank->sectors[i].size);
|
|
|
|
}
|
|
|
|
|
|
|
|
retval = target_read_memory(target, bank->base + bank->sectors[i].offset + j, 4, chunk/4, buffer);
|
|
|
|
if (retval != ERROR_OK)
|
|
|
|
{
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (nBytes = 0; nBytes < chunk; nBytes++)
|
|
|
|
{
|
|
|
|
if (buffer[nBytes] != 0xFF)
|
|
|
|
{
|
|
|
|
bank->sectors[i].is_erased = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
done:
|
|
|
|
free(buffer);
|
|
|
|
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
int default_flash_blank_check(struct flash_bank *bank)
|
|
|
|
{
|
|
|
|
struct target *target = bank->target;
|
|
|
|
int i;
|
|
|
|
int retval;
|
|
|
|
int fast_check = 0;
|
|
|
|
uint32_t blank;
|
|
|
|
|
|
|
|
if (bank->target->state != TARGET_HALTED)
|
|
|
|
{
|
|
|
|
LOG_ERROR("Target not halted");
|
|
|
|
return ERROR_TARGET_NOT_HALTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < bank->num_sectors; i++)
|
|
|
|
{
|
|
|
|
uint32_t address = bank->base + bank->sectors[i].offset;
|
|
|
|
uint32_t size = bank->sectors[i].size;
|
|
|
|
|
2011-10-22 23:12:57 -05:00
|
|
|
retval = target_blank_check_memory(target, address, size, &blank);
|
|
|
|
if (retval != ERROR_OK)
|
2009-12-04 06:37:27 -06:00
|
|
|
{
|
|
|
|
fast_check = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (blank == 0xFF)
|
|
|
|
bank->sectors[i].is_erased = 1;
|
|
|
|
else
|
|
|
|
bank->sectors[i].is_erased = 0;
|
|
|
|
fast_check = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!fast_check)
|
|
|
|
{
|
|
|
|
LOG_USER("Running slow fallback erase check - add working memory");
|
|
|
|
return default_flash_mem_blank_check(bank);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ERROR_OK;
|
|
|
|
}
|
2009-12-18 12:16:52 -06:00
|
|
|
|
2010-01-14 01:33:25 -06:00
|
|
|
/* Manipulate given flash region, selecting the bank according to target
|
|
|
|
* and address. Maps an address range to a set of sectors, and issues
|
|
|
|
* the callback() on that set ... e.g. to erase or unprotect its members.
|
|
|
|
*
|
|
|
|
* (Note a current bad assumption: that protection operates on the same
|
|
|
|
* size sectors as erase operations use.)
|
|
|
|
*
|
|
|
|
* The "pad_reason" parameter is a kind of boolean: when it's NULL, the
|
|
|
|
* range must fit those sectors exactly. This is clearly safe; it can't
|
|
|
|
* erase data which the caller said to leave alone, for example. If it's
|
|
|
|
* non-NULL, rather than failing, extra data in the first and/or last
|
|
|
|
* sectors will be added to the range, and that reason string is used when
|
|
|
|
* warning about those additions.
|
|
|
|
*/
|
2010-11-22 04:16:40 -06:00
|
|
|
static int flash_iterate_address_range_inner(struct target *target,
|
2010-01-14 01:33:25 -06:00
|
|
|
char *pad_reason, uint32_t addr, uint32_t length,
|
2009-12-04 06:01:45 -06:00
|
|
|
int (*callback)(struct flash_bank *bank, int first, int last))
|
|
|
|
{
|
|
|
|
struct flash_bank *c;
|
2009-12-18 12:16:52 -06:00
|
|
|
uint32_t last_addr = addr + length; /* first address AFTER end */
|
2009-12-04 06:01:45 -06:00
|
|
|
int first = -1;
|
|
|
|
int last = -1;
|
|
|
|
int i;
|
|
|
|
|
2010-06-11 01:10:39 -05:00
|
|
|
int retval = get_flash_bank_by_addr(target, addr, true, &c);
|
|
|
|
if (retval != ERROR_OK)
|
|
|
|
return retval;
|
2009-12-04 06:01:45 -06:00
|
|
|
|
|
|
|
if (c->size == 0 || c->num_sectors == 0)
|
|
|
|
{
|
|
|
|
LOG_ERROR("Bank is invalid");
|
|
|
|
return ERROR_FLASH_BANK_INVALID;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (length == 0)
|
|
|
|
{
|
|
|
|
/* special case, erase whole bank when length is zero */
|
|
|
|
if (addr != c->base)
|
2010-01-19 02:47:21 -06:00
|
|
|
{
|
|
|
|
LOG_ERROR("Whole bank access must start at beginning of bank.");
|
2009-12-04 06:01:45 -06:00
|
|
|
return ERROR_FLASH_DST_BREAKS_ALIGNMENT;
|
2010-01-19 02:47:21 -06:00
|
|
|
}
|
2009-12-04 06:01:45 -06:00
|
|
|
|
|
|
|
return callback(c, 0, c->num_sectors - 1);
|
|
|
|
}
|
|
|
|
|
2009-12-18 12:16:52 -06:00
|
|
|
/* check whether it all fits in this bank */
|
2009-12-04 06:01:45 -06:00
|
|
|
if (addr + length - 1 > c->base + c->size - 1)
|
2010-01-19 02:47:21 -06:00
|
|
|
{
|
|
|
|
LOG_ERROR("Flash access does not fit into bank.");
|
2009-12-04 06:01:45 -06:00
|
|
|
return ERROR_FLASH_DST_BREAKS_ALIGNMENT;
|
2010-01-19 02:47:21 -06:00
|
|
|
}
|
2009-12-04 06:01:45 -06:00
|
|
|
|
2009-12-18 12:16:52 -06:00
|
|
|
/** @todo: handle erasures that cross into adjacent banks */
|
|
|
|
|
2009-12-04 06:01:45 -06:00
|
|
|
addr -= c->base;
|
2009-12-28 09:43:51 -06:00
|
|
|
last_addr -= c->base;
|
2009-12-04 06:01:45 -06:00
|
|
|
|
|
|
|
for (i = 0; i < c->num_sectors; i++)
|
|
|
|
{
|
2009-12-18 12:16:52 -06:00
|
|
|
struct flash_sector *f = c->sectors + i;
|
2010-01-14 01:33:25 -06:00
|
|
|
uint32_t end = f->offset + f->size;
|
2009-12-18 12:16:52 -06:00
|
|
|
|
|
|
|
/* start only on a sector boundary */
|
|
|
|
if (first < 0) {
|
2010-01-14 01:33:25 -06:00
|
|
|
/* scanned past the first sector? */
|
|
|
|
if (addr < f->offset)
|
|
|
|
break;
|
|
|
|
|
2009-12-18 12:16:52 -06:00
|
|
|
/* is this the first sector? */
|
|
|
|
if (addr == f->offset)
|
2009-12-04 06:01:45 -06:00
|
|
|
first = i;
|
2010-01-14 01:33:25 -06:00
|
|
|
|
|
|
|
/* Does this need head-padding? If so, pad and warn;
|
|
|
|
* or else force an error.
|
|
|
|
*
|
|
|
|
* Such padding can make trouble, since *WE* can't
|
|
|
|
* ever know if that data was in use. The warning
|
|
|
|
* should help users sort out messes later.
|
|
|
|
*/
|
|
|
|
else if (addr < end && pad_reason) {
|
|
|
|
/* FIXME say how many bytes (e.g. 80 KB) */
|
|
|
|
LOG_WARNING("Adding extra %s range, "
|
|
|
|
"%#8.8x to %#8.8x",
|
|
|
|
pad_reason,
|
|
|
|
(unsigned) f->offset,
|
|
|
|
(unsigned) addr - 1);
|
|
|
|
first = i;
|
|
|
|
} else
|
|
|
|
continue;
|
2009-12-18 12:16:52 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/* is this (also?) the last sector? */
|
2010-01-14 01:33:25 -06:00
|
|
|
if (last_addr == end) {
|
|
|
|
last = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Does this need tail-padding? If so, pad and warn;
|
|
|
|
* or else force an error.
|
|
|
|
*/
|
|
|
|
if (last_addr < end && pad_reason) {
|
|
|
|
/* FIXME say how many bytes (e.g. 80 KB) */
|
|
|
|
LOG_WARNING("Adding extra %s range, "
|
|
|
|
"%#8.8x to %#8.8x",
|
|
|
|
pad_reason,
|
|
|
|
(unsigned) last_addr,
|
|
|
|
(unsigned) end - 1);
|
2009-12-18 12:16:52 -06:00
|
|
|
last = i;
|
|
|
|
break;
|
2009-12-04 06:01:45 -06:00
|
|
|
}
|
2009-12-18 12:16:52 -06:00
|
|
|
|
|
|
|
/* MUST finish on a sector boundary */
|
|
|
|
if (last_addr <= f->offset)
|
|
|
|
break;
|
2009-12-04 06:01:45 -06:00
|
|
|
}
|
|
|
|
|
2009-12-18 12:16:52 -06:00
|
|
|
/* invalid start or end address? */
|
|
|
|
if (first == -1 || last == -1) {
|
|
|
|
LOG_ERROR("address range 0x%8.8x .. 0x%8.8x "
|
|
|
|
"is not sector-aligned",
|
2009-12-19 17:43:55 -06:00
|
|
|
(unsigned) (c->base + addr),
|
2010-01-19 15:56:33 -06:00
|
|
|
(unsigned) (c->base + last_addr - 1));
|
2009-12-18 12:16:52 -06:00
|
|
|
return ERROR_FLASH_DST_BREAKS_ALIGNMENT;
|
|
|
|
}
|
2009-12-04 06:01:45 -06:00
|
|
|
|
2010-01-14 01:33:25 -06:00
|
|
|
/* The NOR driver may trim this range down, based on what
|
|
|
|
* sectors are already erased/unprotected. GDB currently
|
|
|
|
* blocks such optimizations.
|
2009-12-18 12:16:52 -06:00
|
|
|
*/
|
2009-12-04 06:01:45 -06:00
|
|
|
return callback(c, first, last);
|
|
|
|
}
|
|
|
|
|
2010-11-22 04:16:40 -06:00
|
|
|
/* The inner fn only handles a single bank, we could be spanning
|
|
|
|
* multiple chips.
|
|
|
|
*/
|
|
|
|
static int flash_iterate_address_range(struct target *target,
|
|
|
|
char *pad_reason, uint32_t addr, uint32_t length,
|
|
|
|
int (*callback)(struct flash_bank *bank, int first, int last))
|
|
|
|
{
|
|
|
|
struct flash_bank *c;
|
|
|
|
int retval = ERROR_OK;
|
|
|
|
|
|
|
|
/* Danger! zero-length iterations means entire bank! */
|
|
|
|
do
|
|
|
|
{
|
|
|
|
retval = get_flash_bank_by_addr(target, addr, true, &c);
|
|
|
|
if (retval != ERROR_OK)
|
|
|
|
return retval;
|
|
|
|
|
|
|
|
uint32_t cur_length = length;
|
|
|
|
/* check whether it all fits in this bank */
|
|
|
|
if (addr + length - 1 > c->base + c->size - 1)
|
|
|
|
{
|
|
|
|
LOG_DEBUG("iterating over more than one flash bank.");
|
|
|
|
cur_length = c->base + c->size - addr;
|
|
|
|
}
|
|
|
|
retval = flash_iterate_address_range_inner(target,
|
|
|
|
pad_reason, addr, cur_length,
|
|
|
|
callback);
|
|
|
|
if (retval != ERROR_OK)
|
|
|
|
break;
|
|
|
|
|
|
|
|
length -= cur_length;
|
|
|
|
addr += cur_length;
|
|
|
|
} while (length > 0);
|
|
|
|
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2009-12-19 17:43:55 -06:00
|
|
|
int flash_erase_address_range(struct target *target,
|
2010-01-14 01:33:25 -06:00
|
|
|
bool pad, uint32_t addr, uint32_t length)
|
2009-12-04 06:01:45 -06:00
|
|
|
{
|
2010-01-14 01:33:25 -06:00
|
|
|
return flash_iterate_address_range(target, pad ? "erase" : NULL,
|
2009-12-04 06:01:45 -06:00
|
|
|
addr, length, &flash_driver_erase);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int flash_driver_unprotect(struct flash_bank *bank, int first, int last)
|
|
|
|
{
|
|
|
|
return flash_driver_protect(bank, 0, first, last);
|
|
|
|
}
|
|
|
|
|
2010-05-05 02:32:43 -05:00
|
|
|
int flash_unlock_address_range(struct target *target, uint32_t addr, uint32_t length)
|
2009-12-04 06:01:45 -06:00
|
|
|
{
|
2010-01-14 01:33:25 -06:00
|
|
|
/* By default, pad to sector boundaries ... the real issue here
|
|
|
|
* is that our (only) caller *permanently* removes protection,
|
|
|
|
* and doesn't restore it.
|
|
|
|
*/
|
|
|
|
return flash_iterate_address_range(target, "unprotect",
|
2009-12-04 06:01:45 -06:00
|
|
|
addr, length, &flash_driver_unprotect);
|
|
|
|
}
|
|
|
|
|
2010-04-29 10:42:47 -05:00
|
|
|
static int compare_section (const void * a, const void * b)
|
|
|
|
{
|
2010-09-27 09:29:08 -05:00
|
|
|
struct imagesection *b1, *b2;
|
|
|
|
b1=*((struct imagesection **)a);
|
|
|
|
b2=*((struct imagesection **)b);
|
2010-04-29 10:42:47 -05:00
|
|
|
|
|
|
|
if (b1->base_address == b2->base_address)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
} else if (b1->base_address > b2->base_address)
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
} else
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-12-04 06:01:45 -06:00
|
|
|
int flash_write_unlock(struct target *target, struct image *image,
|
|
|
|
uint32_t *written, int erase, bool unlock)
|
|
|
|
{
|
|
|
|
int retval = ERROR_OK;
|
|
|
|
|
|
|
|
int section;
|
|
|
|
uint32_t section_offset;
|
|
|
|
struct flash_bank *c;
|
|
|
|
int *padding;
|
|
|
|
|
|
|
|
section = 0;
|
|
|
|
section_offset = 0;
|
|
|
|
|
|
|
|
if (written)
|
|
|
|
*written = 0;
|
|
|
|
|
|
|
|
if (erase)
|
|
|
|
{
|
|
|
|
/* assume all sectors need erasing - stops any problems
|
|
|
|
* when flash_write is called multiple times */
|
|
|
|
|
|
|
|
flash_set_dirty();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* allocate padding array */
|
2009-12-26 12:22:28 -06:00
|
|
|
padding = calloc(image->num_sections, sizeof(*padding));
|
2009-12-04 06:01:45 -06:00
|
|
|
|
2010-04-29 10:42:47 -05:00
|
|
|
/* This fn requires all sections to be in ascending order of addresses,
|
|
|
|
* whereas an image can have sections out of order. */
|
2010-09-27 09:29:08 -05:00
|
|
|
struct imagesection **sections = malloc(sizeof(struct imagesection *) *
|
2010-04-29 10:42:47 -05:00
|
|
|
image->num_sections);
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < image->num_sections; i++)
|
|
|
|
{
|
|
|
|
sections[i] = &image->sections[i];
|
|
|
|
}
|
|
|
|
|
2010-09-27 09:29:08 -05:00
|
|
|
qsort(sections, image->num_sections, sizeof(struct imagesection *),
|
2010-04-29 10:42:47 -05:00
|
|
|
compare_section);
|
|
|
|
|
2009-12-04 06:01:45 -06:00
|
|
|
/* loop until we reach end of the image */
|
|
|
|
while (section < image->num_sections)
|
|
|
|
{
|
|
|
|
uint32_t buffer_size;
|
|
|
|
uint8_t *buffer;
|
|
|
|
int section_last;
|
2010-04-29 10:42:47 -05:00
|
|
|
uint32_t run_address = sections[section]->base_address + section_offset;
|
|
|
|
uint32_t run_size = sections[section]->size - section_offset;
|
2009-12-04 06:01:45 -06:00
|
|
|
int pad_bytes = 0;
|
|
|
|
|
2010-04-29 10:42:47 -05:00
|
|
|
if (sections[section]->size == 0)
|
2009-12-04 06:01:45 -06:00
|
|
|
{
|
|
|
|
LOG_WARNING("empty section %d", section);
|
|
|
|
section++;
|
|
|
|
section_offset = 0;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* find the corresponding flash bank */
|
2010-06-11 01:10:39 -05:00
|
|
|
retval = get_flash_bank_by_addr(target, run_address, false, &c);
|
|
|
|
if (retval != ERROR_OK)
|
2010-09-27 09:45:25 -05:00
|
|
|
{
|
|
|
|
goto done;
|
|
|
|
}
|
2010-06-11 01:10:39 -05:00
|
|
|
if (c == NULL)
|
2009-12-04 06:01:45 -06:00
|
|
|
{
|
|
|
|
section++; /* and skip it */
|
|
|
|
section_offset = 0;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* collect consecutive sections which fall into the same bank */
|
|
|
|
section_last = section;
|
|
|
|
padding[section] = 0;
|
|
|
|
while ((run_address + run_size - 1 < c->base + c->size - 1)
|
|
|
|
&& (section_last + 1 < image->num_sections))
|
|
|
|
{
|
2010-05-04 00:29:40 -05:00
|
|
|
/* sections are sorted */
|
|
|
|
assert(sections[section_last + 1]->base_address >= c->base);
|
|
|
|
if (sections[section_last + 1]->base_address >= (c->base + c->size))
|
2009-12-04 06:01:45 -06:00
|
|
|
{
|
2010-05-04 00:29:40 -05:00
|
|
|
/* Done with this bank */
|
|
|
|
break;
|
2009-12-04 06:01:45 -06:00
|
|
|
}
|
2009-12-27 13:34:31 -06:00
|
|
|
|
2010-01-08 18:47:58 -06:00
|
|
|
/* FIXME This needlessly touches sectors BETWEEN the
|
2009-12-27 13:34:31 -06:00
|
|
|
* sections it's writing. Without auto erase, it just
|
2010-01-08 18:47:58 -06:00
|
|
|
* writes ones. That WILL INVALIDATE data in cases
|
|
|
|
* like Stellaris Tempest chips, corrupting internal
|
|
|
|
* ECC codes; and at least FreeScale suggests issues
|
|
|
|
* with that approach (in HC11 documentation).
|
2009-12-27 13:34:31 -06:00
|
|
|
*
|
|
|
|
* With auto erase enabled, data in those sectors will
|
|
|
|
* be needlessly destroyed; and some of the limited
|
|
|
|
* number of flash erase cycles will be wasted...
|
|
|
|
*
|
|
|
|
* In both cases, the extra writes slow things down.
|
|
|
|
*/
|
|
|
|
|
2010-01-14 01:33:25 -06:00
|
|
|
/* if we have multiple sections within our image,
|
|
|
|
* flash programming could fail due to alignment issues
|
2009-12-04 06:01:45 -06:00
|
|
|
* attempt to rebuild a consecutive buffer for the flash loader */
|
2010-04-29 10:42:47 -05:00
|
|
|
pad_bytes = (sections[section_last + 1]->base_address) - (run_address + run_size);
|
2009-12-04 06:01:45 -06:00
|
|
|
padding[section_last] = pad_bytes;
|
2010-04-29 10:42:47 -05:00
|
|
|
run_size += sections[++section_last]->size;
|
2009-12-04 06:01:45 -06:00
|
|
|
run_size += pad_bytes;
|
|
|
|
|
2010-04-28 01:01:28 -05:00
|
|
|
if (pad_bytes > 0)
|
|
|
|
LOG_INFO("Padding image section %d with %d bytes", section_last-1, pad_bytes);
|
2009-12-04 06:01:45 -06:00
|
|
|
}
|
|
|
|
|
2010-06-09 10:12:52 -05:00
|
|
|
if (run_address + run_size - 1 > c->base + c->size - 1)
|
|
|
|
{
|
2010-11-15 07:43:16 -06:00
|
|
|
/* If we have more than one flash chip back to back, then we limit
|
|
|
|
* the current write operation to the current chip.
|
|
|
|
*/
|
|
|
|
LOG_DEBUG("Truncate flash run size to the current flash chip.");
|
|
|
|
|
|
|
|
run_size = c->base + c->size - run_address;
|
|
|
|
assert(run_size > 0);
|
2010-06-09 10:12:52 -05:00
|
|
|
}
|
2009-12-04 06:01:45 -06:00
|
|
|
|
2009-12-27 13:34:31 -06:00
|
|
|
/* If we're applying any sector automagic, then pad this
|
|
|
|
* (maybe-combined) segment to the end of its last sector.
|
|
|
|
*/
|
|
|
|
if (unlock || erase) {
|
|
|
|
int sector;
|
|
|
|
uint32_t offset_start = run_address - c->base;
|
|
|
|
uint32_t offset_end = offset_start + run_size;
|
|
|
|
uint32_t end = offset_end, delta;
|
|
|
|
|
|
|
|
for (sector = 0; sector < c->num_sectors; sector++) {
|
|
|
|
end = c->sectors[sector].offset
|
|
|
|
+ c->sectors[sector].size;
|
|
|
|
if (offset_end <= end)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
delta = end - offset_end;
|
|
|
|
padding[section_last] += delta;
|
|
|
|
run_size += delta;
|
|
|
|
}
|
|
|
|
|
2009-12-04 06:01:45 -06:00
|
|
|
/* allocate buffer */
|
|
|
|
buffer = malloc(run_size);
|
2010-09-27 09:45:25 -05:00
|
|
|
if (buffer == NULL)
|
|
|
|
{
|
|
|
|
LOG_ERROR("Out of memory for flash bank buffer");
|
|
|
|
retval = ERROR_FAIL;
|
|
|
|
goto done;
|
|
|
|
}
|
2009-12-04 06:01:45 -06:00
|
|
|
buffer_size = 0;
|
|
|
|
|
|
|
|
/* read sections to the buffer */
|
|
|
|
while (buffer_size < run_size)
|
|
|
|
{
|
|
|
|
size_t size_read;
|
|
|
|
|
|
|
|
size_read = run_size - buffer_size;
|
2010-04-29 10:42:47 -05:00
|
|
|
if (size_read > sections[section]->size - section_offset)
|
|
|
|
size_read = sections[section]->size - section_offset;
|
2009-12-04 06:01:45 -06:00
|
|
|
|
2010-04-29 10:42:47 -05:00
|
|
|
/* KLUDGE!
|
|
|
|
*
|
|
|
|
* #¤%#"%¤% we have to figure out the section # from the sorted
|
|
|
|
* list of pointers to sections to invoke image_read_section()...
|
|
|
|
*/
|
2010-05-04 00:29:40 -05:00
|
|
|
intptr_t diff = (intptr_t)sections[section] - (intptr_t)image->sections;
|
2010-09-27 09:29:08 -05:00
|
|
|
int t_section_num = diff / sizeof(struct imagesection);
|
2010-04-29 10:42:47 -05:00
|
|
|
|
2010-05-24 06:30:29 -05:00
|
|
|
LOG_DEBUG("image_read_section: section = %d, t_section_num = %d, section_offset = %d, buffer_size = %d, size_read = %d",
|
2010-05-04 00:29:40 -05:00
|
|
|
(int)section,
|
|
|
|
(int)t_section_num, (int)section_offset, (int)buffer_size, (int)size_read);
|
2010-04-29 10:42:47 -05:00
|
|
|
if ((retval = image_read_section(image, t_section_num, section_offset,
|
2009-12-04 06:01:45 -06:00
|
|
|
size_read, buffer + buffer_size, &size_read)) != ERROR_OK || size_read == 0)
|
|
|
|
{
|
|
|
|
free(buffer);
|
2010-04-29 10:42:47 -05:00
|
|
|
goto done;
|
2009-12-04 06:01:45 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/* see if we need to pad the section */
|
|
|
|
while (padding[section]--)
|
|
|
|
(buffer + buffer_size)[size_read++] = 0xff;
|
|
|
|
|
|
|
|
buffer_size += size_read;
|
|
|
|
section_offset += size_read;
|
|
|
|
|
2010-04-29 10:42:47 -05:00
|
|
|
if (section_offset >= sections[section]->size)
|
2009-12-04 06:01:45 -06:00
|
|
|
{
|
|
|
|
section++;
|
|
|
|
section_offset = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
retval = ERROR_OK;
|
|
|
|
|
|
|
|
if (unlock)
|
|
|
|
{
|
|
|
|
retval = flash_unlock_address_range(target, run_address, run_size);
|
|
|
|
}
|
|
|
|
if (retval == ERROR_OK)
|
|
|
|
{
|
|
|
|
if (erase)
|
|
|
|
{
|
|
|
|
/* calculate and erase sectors */
|
2010-01-14 01:33:25 -06:00
|
|
|
retval = flash_erase_address_range(target,
|
2010-04-28 20:49:32 -05:00
|
|
|
true, run_address, run_size);
|
2009-12-04 06:01:45 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (retval == ERROR_OK)
|
|
|
|
{
|
|
|
|
/* write flash sectors */
|
|
|
|
retval = flash_driver_write(c, buffer, run_address - c->base, run_size);
|
|
|
|
}
|
|
|
|
|
|
|
|
free(buffer);
|
|
|
|
|
|
|
|
if (retval != ERROR_OK)
|
|
|
|
{
|
2010-04-29 10:42:47 -05:00
|
|
|
/* abort operation */
|
|
|
|
goto done;
|
2009-12-04 06:01:45 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
if (written != NULL)
|
|
|
|
*written += run_size; /* add run size to total written counter */
|
|
|
|
}
|
|
|
|
|
2010-04-29 10:42:47 -05:00
|
|
|
|
|
|
|
done:
|
|
|
|
free(sections);
|
2009-12-04 06:01:45 -06:00
|
|
|
free(padding);
|
|
|
|
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
int flash_write(struct target *target, struct image *image,
|
|
|
|
uint32_t *written, int erase)
|
|
|
|
{
|
|
|
|
return flash_write_unlock(target, image, written, erase, false);
|
|
|
|
}
|