Skip to contentSkip to conversation

dfu: dfu_sf: Take the start address into account

PATCH In review Fabio Estevam 4 participants 7 messages Sep 23, 2015 – Oct 20, 2015

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 dfu: dfu_sf: Take the start address into account

From: Fabio Estevam <[email protected]>

The dfu_alt_info_spl variable allows passing a starting point for the binary to be flashed in the SPI NOR.

For example, if we have 'dfu_alt_info_spl=spl raw 0x400', this means that we want to flash the binary starting at address 0x400.

In order to do so we need to erase the entire sector and write to the the subsequent SPI NOR sectors taking such start address into account for the address calculations.

Tested by succesfully writing SPL binary into 0x400 offset and the u-boot.img at offset 64 kiB of a SPL NOR.

---
 drivers/dfu/dfu_sf.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)
drivers/dfu/dfu_sf.c +10 -2
index 448d95d..844da1f 100644
@@ -23,17 +23,25 @@ static int dfu_read_medium_sf(struct dfu_entity *dfu, u64 offset, void *buf,
 	return spi_flash_read(dfu->data.sf.dev, offset, *len, buf);
 }
 
+static u64 find_sector(struct dfu_entity *dfu, u64 start, u64 offset)
+{
+	return ((start + offset) / dfu->data.sf.dev->sector_size) *
+		dfu->data.sf.dev->sector_size;
+}
+
 static int dfu_write_medium_sf(struct dfu_entity *dfu,
 		u64 offset, void *buf, long *len)
 {
 	int ret;
 
-	ret = spi_flash_erase(dfu->data.sf.dev, offset,
+	ret = spi_flash_erase(dfu->data.sf.dev,
+			      find_sector(dfu, dfu->data.sf.start, offset),
 			      dfu->data.sf.dev->sector_size);
 	if (ret)
 		return ret;
 
-	ret = spi_flash_write(dfu->data.sf.dev, offset, *len, buf);
+	ret = spi_flash_write(dfu->data.sf.dev, dfu->data.sf.start + offset,
+			      *len, buf);
 	if (ret)
 		return ret;
 
Signature · 1 line
1.9.1
#1 lore raw

PATCH 1/1 dfu: dfu_sf: Take the start address into account

Hi Fabio,

Quoted 43 lines

From: Fabio Estevam <[email protected]>

The dfu_alt_info_spl variable allows passing a starting point for the binary to be flashed in the SPI NOR.

For example, if we have 'dfu_alt_info_spl=spl raw 0x400', this means that we want to flash the binary starting at address 0x400.

In order to do so we need to erase the entire sector and write to the the subsequent SPI NOR sectors taking such start address into account for the address calculations.

Tested by succesfully writing SPL binary into 0x400 offset and the u-boot.img at offset 64 kiB of a SPL NOR.

---
 drivers/dfu/dfu_sf.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)
drivers/dfu/dfu_sf.c +8 -1
index 448d95d..844da1f 100644
@@ -23,17 +23,25 @@ static int dfu_read_medium_sf(struct dfu_entity
 *dfu, u64 offset, void *buf, return spi_flash_read(dfu->data.sf.dev,
 offset, *len, buf); }
 
+static u64 find_sector(struct dfu_entity *dfu, u64 start, u64 offset)
+{
+	return ((start + offset) / dfu->data.sf.dev->sector_size) *
+		dfu->data.sf.dev->sector_size;
+}
+
 static int dfu_write_medium_sf(struct dfu_entity *dfu,
 		u64 offset, void *buf, long *len)
 {
 	int ret;
 
-	ret = spi_flash_erase(dfu->data.sf.dev, offset,
+	ret = spi_flash_erase(dfu->data.sf.dev,
+			      find_sector(dfu, dfu->data.sf.start,

drivers/dfu/dfu_sf.c static int dfu_read_medium_sf(struct dfu_entity · line 47

Quoted 10 lines
drivers/dfu/dfu_sf.c +2 -1
 offset), dfu->data.sf.dev->sector_size);
 	if (ret)
 		return ret;
 
-	ret = spi_flash_write(dfu->data.sf.dev, offset, *len, buf);
+	ret = spi_flash_write(dfu->data.sf.dev, dfu->data.sf.start +
 offset,
+			      *len, buf);
 	if (ret)
 		return ret;

Applied to u-boot-dfu.

Thanks for your patch.

Signature · 5 lines
Best regards,

Lukasz Majewski

Samsung R&D Institute Poland (SRPOL) | Linux Platform Group
#2 lore raw

PATCH 1/1 dfu: dfu_sf: Take the start address into account

In reply to Fabio Estevam
Quoted 8 lines · Fabio Estevam

From: Fabio Estevam <[email protected]>

The dfu_alt_info_spl variable allows passing a starting point for the binary to be flashed in the SPI NOR.

For example, if we have 'dfu_alt_info_spl=spl raw 0x400', this means that we want to flash the binary starting at address 0x400.

Quoted 6 lines

In order to do so we need to erase the entire sector and write to the the subsequent SPI NOR sectors taking such start address into account for the address calculations.

Tested by succesfully writing SPL binary into 0x400 offset and the u-boot.img at offset 64 kiB of a SPL NOR.

This feels wrong. The entries in the DFU list must already be sector-aligned, so there's no need for the code to round them. If the entries are not already aligned, then the aligned erase operations will erase other data, which will cause corruption.

#3 lore raw

PATCH 1/1 dfu: dfu_sf: Take the start address into account

Quoted 4 lines · Stephen Warren

This feels wrong. The entries in the DFU list must already be sector-aligned, so there's no need for the code to round them. If the entries are not already aligned, then the aligned erase operations will erase other data, which will cause corruption.

The start address does not need to be sector-aligned.

In my case the SPL image needs to be placed at 0x400.

#4 lore raw

PATCH 1/1 dfu: dfu_sf: Take the start address into account

Quoted 10 lines · Fabio Estevam
Quoted 4 lines · Stephen Warren

This feels wrong. The entries in the DFU list must already be sector-aligned, so there's no need for the code to round them. If the entries are not already aligned, then the aligned erase operations will erase other data, which will cause corruption.

The start address does not need to be sector-aligned.

In my case the SPL image needs to be placed at 0x400.

Also, just to clarify: in this patch we make sure that the erase operation are always sector-aligned.

Only the write operations can be 'shifted' due to the start address passed via dfu_alt_info.

#5 lore raw

PATCH 1/1 dfu: dfu_sf: Take the start address into account

Quoted 10 lines · Fabio Estevam
Quoted 9 lines · Fabio Estevam
Quoted 4 lines · Stephen Warren

This feels wrong. The entries in the DFU list must already be sector-aligned, so there's no need for the code to round them. If the entries are not already aligned, then the aligned erase operations will erase other data, which will cause corruption.

The start address does not need to be sector-aligned.

Quoted 7 lines
Quoted 1 line

In my case the SPL image needs to be placed at 0x400.

Also, just to clarify: in this patch we make sure that the erase operation are always sector-aligned.

Only the write operations can be 'shifted' due to the start address passed via dfu_alt_info.

So it's OK to erase the data between 0..0x400 and not replace it? Even if that's true in your case, it seems quite unlikely in general.

Instead, the DFU region should start and end at sector-aligned locations so that erasing the region doesn't negatively affect other data. If logical objects in your flash aren't sector aligned, then that simply means you need to construct a complete flash image and write the whole thing at once. I don't thin there's any other way to avoid corruption.

#6 lore raw
In reply to Fabio Estevam
Quoted 12 lines · Fabio Estevam

From: Fabio Estevam <[email protected]>

The dfu_alt_info_spl variable allows passing a starting point for the binary to be flashed in the SPI NOR.

For example, if we have 'dfu_alt_info_spl=spl raw 0x400', this means that we want to flash the binary starting at address 0x400.

In order to do so we need to erase the entire sector and write to the the subsequent SPI NOR sectors taking such start address into account for the address calculations.

Quoted 5 lines

Tested by succesfully writing SPL binary into 0x400 offset and the u-boot.img at offset 64 kiB of a SPL NOR.

After making this use lldiv() for the math,
Applied to u-boot/master, thanks!

Signature · 8 lines
Tom
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 836 bytes
Desc: Digital signature
URL: <http://lists.denx.de/pipermail/u-boot/attachments/20151019/1544f083/attachment.sig>