Skip to contentSkip to conversation

crash when doing `sf probe` multiple times due to API miscommunication

U-Boot New Mike Frysinger 3 participants 6 messages Dec 11, 2008 – Jan 24, 2009

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

the sf probe command does:
static int do_spi_flash_probe(...)

{
	...
	new = spi_flash_probe(bus, cs, speed, mode);
	if (flash)
		spi_flash_free(flash);
	flash = new;
	...
}

looks good ... if the user ran sf probe once already, then we need to free that structure ...

however, let's take a look at spi_flash_probe ...
struct spi_flash *spi_flash_probe(...)

{
	...
	case 0x01: flash = spi_flash_probe_atmel(spi, idcode);
	...
	case 0x1F: flash = spi_flash_probe_spansion(spi, idcode);
	...
	case 0x20: flash = spi_flash_probe_stmicro(spi, idcode);
	...
	return flash;
}

and so we descend another level ...
struct spi_flash *spi_flash_probe_stmicro(...)

{
	...
	stm = malloc(sizeof(struct stmicro_spi_flash));
	...
	return &stm->flash;
}
struct spi_flash *spi_flash_probe_atmel(...)
{
	...
	asf = malloc(sizeof(struct atmel_spi_flash));
	...
	return &asf->flash;
}

clearly this isnt lining up. the sf command expects to be given back malloced memory, not a pointer to the middle of a malloc. so calling free() on the pointer returned is invalid (and in my case, crashes the board most of the time).
-mike
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 835 bytes
Desc: This is a digitally signed message part.
Url : http://lists.denx.de/pipermail/u-boot/attachments/20081211/5430e1ea/attachment-0001.pgp

#1 lore raw
Quoted 10 lines · On Thursday 11 December 2008 15:52:25 Mike Frysinger wrote:

the sf probe command does:
static int do_spi_flash_probe(...)

{
	...
	new = spi_flash_probe(bus, cs, speed, mode);
	if (flash)
		spi_flash_free(flash);
	flash = new;
	...
}

oh, i left out one last function ...
void spi_flash_free(struct spi_flash *flash)

{
	spi_free_slave(flash->spi);
	free(flash);
}

and we see the wrong "free(flash)" ...

maybe if the spi_flash structure provided a pointer to its parent, then we could do "free(flash->parent)" ...
-mike
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 835 bytes
Desc: This is a digitally signed message part.
Url : http://lists.denx.de/pipermail/u-boot/attachments/20081211/38bbef3e/attachment.pgp

#2 lore raw

PATCH 1/1 spi flash: fix crash due to spi flash miscommunication

In reply to Mike Frysinger

From: Brad Bozarth <[email protected]>

Higher spi flash layers expect to be given back a pointer that was malloced so that it can free the result, but the lower layers return a pointer that is in the middle of the malloced memory. Reorder the members of the lower spi structures so that things work out.

---
Jason McMullan: your winbond driver will need a similar change:

	drivers/mtd/spi/winbond.c
	+/* spi_flash needs to be first so upper layers can free() it */
	 struct winbond_spi_flash {
	-	const struct winbond_spi_flash_params *params;
	 	struct spi_flash flash;
	+	const struct winbond_spi_flash_params *params;
	 };
 drivers/mtd/spi/atmel.c   |    3 ++-
 drivers/mtd/spi/stmicro.c |    3 ++-
 2 files changed, 4 insertions(+), 2 deletions(-)
drivers/mtd/spi/atmel.c +2 -1
index 10fcf0c..a5f51ca 100644
@@ -39,9 +39,10 @@ struct atmel_spi_flash_params {
 	const char	*name;
 };
 
+/* spi_flash needs to be first so upper layers can free() it */
 struct atmel_spi_flash {
-	const struct atmel_spi_flash_params *params;
 	struct spi_flash flash;
+	const struct atmel_spi_flash_params *params;
 };
 
 static inline struct atmel_spi_flash *
drivers/mtd/spi/stmicro.c +2 -1
index 86324e4..e7dda91 100644
@@ -64,9 +64,10 @@ struct stmicro_spi_flash_params {
 	const char *name;
 };
 
+/* spi_flash needs to be first so upper layers can free() it */
 struct stmicro_spi_flash {
-	const struct stmicro_spi_flash_params *params;
 	struct spi_flash flash;
+	const struct stmicro_spi_flash_params *params;
 };
 
 static inline struct stmicro_spi_flash *to_stmicro_spi_flash(struct spi_flash
Signature · 1 line
1.6.0.6
#3 lore raw

PATCH 1/1 spi flash: fix crash due to spi flash miscommunication

Quoted 10 lines · Mike Frysinger

From: Brad Bozarth <[email protected]>

Higher spi flash layers expect to be given back a pointer that was malloced so that it can free the result, but the lower layers return a pointer that is in the middle of the malloced memory. Reorder the members of the lower spi structures so that things work out.

#4 lore raw

PATCH 1/1 spi flash: fix crash due to spi flash miscommunication

Quoted 8 lines · On Monday 05 January 2009 03:17:00 Haavard Skinnemoen wrote:
Quoted 7 lines · Mike Frysinger

From: Brad Bozarth <[email protected]>

Higher spi flash layers expect to be given back a pointer that was malloced so that it can free the result, but the lower layers return a pointer that is in the middle of the malloced memory. Reorder the members of the lower spi structures so that things work out.

Quoted 5 lines
Quoted 3 lines

Wolfgang: this should be in for the next release ...
-mike
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 835 bytes
Desc: This is a digitally signed message part.
Url : http://lists.denx.de/pipermail/u-boot/attachments/20090118/2e48b7c9/attachment.pgp

#5 lore raw

PATCH 1/1 spi flash: fix crash due to spi flash miscommunication

In reply to Mike Frysinger

Dear Mike Frysinger,

Quoted 20 lines · In message <[email protected]> you wrote:

From: Brad Bozarth <[email protected]>

Higher spi flash layers expect to be given back a pointer that was malloced so that it can free the result, but the lower layers return a pointer that is in the middle of the malloced memory. Reorder the members of the lower spi structures so that things work out.

---
Jason McMullan: your winbond driver will need a similar change:

	drivers/mtd/spi/winbond.c
	+/* spi_flash needs to be first so upper layers can free() it */
	 struct winbond_spi_flash {
	-	const struct winbond_spi_flash_params *params;
	 	struct spi_flash flash;
	+	const struct winbond_spi_flash_params *params;
	 };
Quoted 3 lines
 drivers/mtd/spi/atmel.c   |    3 ++-
 drivers/mtd/spi/stmicro.c |    3 ++-
 2 files changed, 4 insertions(+), 2 deletions(-)

Applied, thanks.

Best regards,

Wolfgang Denk

Signature · 5 lines
DENX Software Engineering GmbH,     MD: Wolfgang Denk & Detlev Zundel
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-10 Fax: (+49)-8142-66989-80 Email: wd at denx.de
It is dangerous to be sincere unless you are also stupid.
                                                - George Bernard Shaw