Fix flash_dataptr() to support flash at zero

If flash starts at zero, then flash_dataptr() will return NULL for a valid
region. Change the function around so that it can be used in this case.

BUG=chrome-os-partner:10146
TEST=manual:
Modify code it print out parameters and problems, then:
Writing 256 bytes to 0x0...
0 256 64
ok 131072
Command returned error 1
> flashwrite 0 256
Writing 256 bytes to 0x0...
0 256 64
ok 131072
Command returned error 1
> flashwrite 0 255
Writing 255 bytes to 0x0...
0 255 64
Command usage/param invalid.
Usage: flashwrite offset [size]

Change-Id: I5683fc181ef780310ceff50f120735659e973784
Signed-off-by: Simon Glass <sjg@chromium.org>
Reviewed-on: https://gerrit.chromium.org/gerrit/26749
Reviewed-by: David Hendricks <dhendrix@chromium.org>
Reviewed-by: Rong Chang <rongchang@chromium.org>
This commit is contained in:
Simon Glass
2012-07-03 20:03:23 -07:00
committed by Gerrit
parent 9a4205faf3
commit a60fdbdfcb
2 changed files with 20 additions and 18 deletions

View File

@@ -136,22 +136,22 @@ int flash_get_size(void)
}
char *flash_dataptr(int offset, int size_req, int align, int *sizep)
int flash_dataptr(int offset, int size_req, int align, char **ptrp)
{
if (offset < 0 || size_req < 0 ||
offset + size_req > usable_flash_size ||
(offset | size_req) & (align - 1))
return NULL; /* Invalid range */
if (sizep)
*sizep = usable_flash_size - offset;
return -1; /* Invalid range */
if (ptrp)
*ptrp = flash_physical_dataptr(offset);
return flash_physical_dataptr(offset);
return usable_flash_size - offset;
}
int flash_read(int offset, int size, char *data)
{
if (!flash_dataptr(offset, size, 1, NULL))
if (flash_dataptr(offset, size, 1, NULL) < 0)
return EC_ERROR_INVAL; /* Invalid range */
return flash_physical_read(offset, size, data);
@@ -160,7 +160,8 @@ int flash_read(int offset, int size, char *data)
int flash_write(int offset, int size, const char *data)
{
if (!flash_dataptr(offset, size, flash_get_write_block_size(), NULL))
if (flash_dataptr(offset, size, flash_get_write_block_size(),
NULL) < 0)
return EC_ERROR_INVAL; /* Invalid range */
/* TODO (crosbug.com/p/7478) - safety check - don't allow writing to
@@ -172,7 +173,8 @@ int flash_write(int offset, int size, const char *data)
int flash_erase(int offset, int size)
{
if (!flash_dataptr(offset, size, flash_get_erase_block_size(), NULL))
if (flash_dataptr(offset, size, flash_get_erase_block_size(),
NULL) < 0)
return EC_ERROR_INVAL; /* Invalid range */
/* TODO (crosbug.com/p/7478) - safety check - don't allow erasing the
@@ -187,7 +189,7 @@ int flash_protect_until_reboot(int offset, int size)
int pbsize = flash_get_protect_block_size();
int i;
if (!flash_dataptr(offset, size, pbsize, NULL))
if (flash_dataptr(offset, size, pbsize, NULL) < 0)
return EC_ERROR_INVAL; /* Invalid range */
/* Convert offset and size to blocks */
@@ -206,7 +208,7 @@ int flash_set_protect(int offset, int size, int enable)
int pbsize = flash_get_protect_block_size();
int rv, i;
if (!flash_dataptr(offset, size, pbsize, NULL))
if (flash_dataptr(offset, size, pbsize, NULL) < 0)
return EC_ERROR_INVAL; /* Invalid range */
/* Fail if write protect block is already locked */
@@ -292,7 +294,7 @@ int flash_get_protect(int offset, int size)
uint8_t minflags = 0xff;
int i;
if (!flash_dataptr(offset, size, pbsize, NULL))
if (flash_dataptr(offset, size, pbsize, NULL) < 0)
return 0; /* Invalid range; assume nothing protected */
/* Convert offset and size to blocks */

View File

@@ -73,19 +73,19 @@ int flash_get_size(void);
* contiguous from this start address through to the end of the usable
* flash.
*
* This function returns NULL if offset + size_req extends beyond the end
* of flash, or if either size_req or offset are not aligned to 'align'.
* This function returns -1 if offset + size_req extends beyond the end
* of flash, the offset is out of range, or if either size_req or offset
* are not aligned to 'align'.
*
* @param offset Flash offset to get address of
* @param size_req Number of bytes requested
* @param align Ensure offset and size_req are aligned to given
* power of two.
* @param sizep If not NULL, returns amount of flash available at
* this memory addr, unless function fails, iwc it is
* unset.
* @return pointer to flash, or NULL on error
* @param ptrp If not NULL, returns a pointer to this flash offset
* in memory, unless function fails, iwc it is unset.
* @return size of flash region available at *ptrp, or -1 on error
*/
char *flash_dataptr(int offset, int size_req, int align, int *sizep);
int flash_dataptr(int offset, int size_req, int align, char **ptrp);
/* Reads <size> bytes of data from offset <offset> into <data>. */
int flash_read(int offset, int size, char *data);