Skip to contentSkip to conversation

dfu: initial implementation

PATCH New Andrzej Pietrasiewicz 5 participants 26 messages Nov 2, 2011 – Nov 8, 2011 USB

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 dfu: initial implementation

---

Dear All,

This is Device Firmware Upgrade (DFU) implementation which supports data upload and download function to devices which are equipped with a UDC.

Device Firmware Upgrade (DFU) is an extension to the USB specification. As of the time of this writing it is documented at

http://www.usb.org/developers/devclass_docs/DFU_1.1.pdf

The aim of DFU is to allow transferring data to/from selected areas of interest within a compliant device. In the DFU nomenclature the host->device direction is called download and the device->host direction is called upload. The areas are defined by the compliant device. In the DFU nomenclature the area of interest within the device is called an altsetting. The device is connected to the host through USB. On the host side compliant software must be used to initiate the data transfer. Example piece of such software is dfu-util package from the OpenMoko project:

http://wiki.openmoko.org/wiki/Dfu-util.

Please refer to dfu-util documentation for details.
The below ASCII-art presents a connection between the host and the device.

      +-----------------+    <--- DFU --->    +-------------------------+
      |  e.g. dfu-util  |    <=== USB ===>    |          / altsetting 0 |
      |          host   +---------------------+   device - altsetting 1 |
      |   file /        |                     |          \     ...      |
      +-----------------+                     +-------------------------+

The DFU implementation on the device side remains in one of the two modes:
the Run-Time mode and the DFU mode. The USB descriptor sets exported by the device depend on which mode is in force. While in DFU mode the device exports the descriptors corresponding to each available altsetting. An example dfu-util session when the device is in the DFU mode looks similar to this:

# dfu-util -l
dfu-util - (C) 2007-2008 by OpenMoko Inc.
This program is Free Software and has ABSOLUTELY NO WARRANTY

Found DFU: [0x0525:0xffff] devnum=46, cfg=0, intf=0, alt=0, name="bootldr" Found DFU: [0x0525:0xffff] devnum=46, cfg=0, intf=0, alt=1, name="kernel"

In the above example there are two altsettings, altsetting 0 with a human-readable name "bootldr" and altsetting 1 with a human-readable name "kernel"

To initiate data transfer the user at the host side must specify the altsetting to/from which the data transfer is to be performed. In case of the dfu-util it is specified with a "-a" option followed by either a number or a human-readable name. The user also needs to specify the direction of the data transfer and the file on the host from/to which data are to be transfered. An example download command line looks similar to this:

# dfu-util -D vmlinux -a kernel

In the above command line the contents of the file vmlinux is to be downloaded into the altsetting named kernel.

An example upload command line looks similar to this:

# dfu-util -U vmlinux -a kernel

In the above command line the contents of the altsetting named kernel is to be uploaded into the file vmlinux.

This u-boot implementation introduces a parameter-less dfu command. After the user finishes with dfu they can Ctrl-C to return to u-boot main menu.

The implementation is split into two parts: the dfu gadget implementation and a "flashing backend", the interface between the two being the struct flash_entity. The "flashing backend"'s implementation is board-specific and is implemented on the Goni target.

Patch summary:

Andrzej Pietrasiewicz (1):
dfu: initial implementation

 board/samsung/goni/Makefile |    2 +
 board/samsung/goni/flash.c  |  634 +++++++++++++++++++++++++++++
 board/samsung/goni/flash.h  |   28 ++
 board/samsung/goni/goni.c   |   17 +
 common/Makefile             |    1 +
 common/cmd_dfu.c            |   50 +++
 drivers/usb/gadget/Makefile |    1 +
 drivers/usb/gadget/dfu.c    |  920 +++++++++++++++++++++++++++++++++++++++++++
 drivers/usb/gadget/dfu.h    |  171 ++++++++
 include/configs/s5p_goni.h  |    6 +
 include/dfu.h               |   31 ++
 include/flash_entity.h      |   39 ++
 include/mbr.h               |   49 +++
 13 files changed, 1949 insertions(+), 0 deletions(-)
 create mode 100644 board/samsung/goni/flash.c
 create mode 100644 board/samsung/goni/flash.h
 create mode 100644 common/cmd_dfu.c
 create mode 100644 drivers/usb/gadget/dfu.c
 create mode 100644 drivers/usb/gadget/dfu.h
 create mode 100644 include/dfu.h
 create mode 100644 include/flash_entity.h
 create mode 100644 include/mbr.h
board/samsung/goni/Makefile +2 -0
index ecde7a7..c07b0a2 100644
@@ -31,6 +31,8 @@ LIB	= $(obj)lib$(BOARD).o
 COBJS-y	:= goni.o onenand.o
 SOBJS	:= lowlevel_init.o
 
+COBJS-$(CONFIG_CMD_DEVICE_FIRMWARE_UPGRADE) += flash.o
+
 SRCS    := $(SOBJS:.o=.S) $(COBJS-y:.o=.c)
 OBJS	:= $(addprefix $(obj),$(COBJS-y))
 SOBJS	:= $(addprefix $(obj),$(SOBJS))
board/samsung/goni/flash.c new file +634 -0

634 lines not on this page · raw message

board/samsung/goni/flash.h new file +28 -0
new file mode 100644
index 0000000..ffe4d6d
@@ -0,0 +1,28 @@
+/*
+ * flash.h -- board flashing routines interface
+ *
+ * Copyright (C) 2011 Samsung Electronics
+ * author: Andrzej Pietrasiewicz <[email protected]>
+ *
+ * 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
+ */
+
+#ifndef FLASH_GONI_H_
+#define FLASH_GONI_H_
+
+void register_flash_areas(void);
+void unregister_flash_areas(void);
+
+#endif
board/samsung/goni/goni.c +17 -0
index c4fc3e9..da9afd0 100644
@@ -29,6 +29,9 @@
 #include <asm/arch/hs_otg.h>
 #include <asm/arch/cpu.h>
 #include <max8998_pmic.h>
+
+#include "flash.h"
+
 DECLARE_GLOBAL_DATA_PTR;
 
 static struct s5pc110_gpio *s5pc110_gpio;
@@ -144,4 +147,18 @@ struct s3c_plat_otg_data s5pc110_otg_data = {
 	.regs_phy = S5PC110_PHY_BASE,
 	.regs_otg = S5PC110_OTG_BASE,
 };
+
+#ifdef CONFIG_CMD_DEVICE_FIRMWARE_UPGRADE
+void board_dfu_init(void)
+{
+	register_flash_areas();
+	s3c_udc_probe(&s5pc110_otg_data);
+}
+
+void board_dfu_cleanup(void)
+{
+	unregister_flash_areas();
+}
+#endif
+
 #endif
common/Makefile +1 -0
index ae795e0..de926d9 100644
@@ -162,6 +162,7 @@ COBJS-y += cmd_usb.o
 COBJS-y += usb.o
 COBJS-$(CONFIG_USB_STORAGE) += usb_storage.o
 endif
+COBJS-$(CONFIG_CMD_DEVICE_FIRMWARE_UPGRADE) += cmd_dfu.o
 COBJS-$(CONFIG_CMD_XIMG) += cmd_ximg.o
 COBJS-$(CONFIG_YAFFS2) += cmd_yaffs2.o
 
common/cmd_dfu.c new file +50 -0

50 lines not on this page · raw message

drivers/usb/gadget/Makefile +1 -0
index cd22bbe..4b173e2 100644
@@ -28,6 +28,7 @@ LIB	:= $(obj)libusb_gadget.o
 # new USB gadget layer dependencies
 ifdef CONFIG_USB_GADGET
 COBJS-y += epautoconf.o config.o usbstring.o
+COBJS-$(CONFIG_DFU_GADGET) += dfu.o
 COBJS-$(CONFIG_USB_GADGET_S3C_UDC_OTG) += s3c_udc_otg.o
 endif
 ifdef CONFIG_USB_ETHER
drivers/usb/gadget/dfu.c new file +920 -0

920 lines not on this page · raw message

drivers/usb/gadget/dfu.h new file +171 -0

171 lines not on this page · raw message

include/configs/s5p_goni.h +6 -0
index df97802..186da76 100644
@@ -86,6 +86,8 @@
 #define CONFIG_CMD_ONENAND
 #define CONFIG_CMD_MTDPARTS
 #define CONFIG_CMD_MMC
+#define CONFIG_CMD_FAT
+#define CONFIG_CMD_DEVICE_FIRMWARE_UPGRADE
 
 #define CONFIG_BOOTDELAY		1
 #define CONFIG_ZERO_BOOTDELAY_CHECK
@@ -241,4 +243,8 @@
 #define CONFIG_USB_GADGET_S3C_UDC_OTG	1
 #define CONFIG_USB_GADGET_DUALSPEED	1
 
+#if defined (CONFIG_CMD_DEVICE_FIRMWARE_UPGRADE)
+#define CONFIG_DFU_GADGET
+#endif
+
 #endif	/* __CONFIG_H */
include/dfu.h new file +31 -0
new file mode 100644
index 0000000..977ca91
@@ -0,0 +1,31 @@
+/*
+ * dfu.h - Device Firmware Upgrade
+ *
+ * copyright (c) 2011 samsung electronics
+ * author: andrzej pietrasiewicz <[email protected]>
+ *
+ * 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
+ */
+
+#ifndef __DFU_H__
+#define __DFU_H__
+
+extern void board_dfu_init(void);
+extern int dfu_init(void);
+extern int dfu_cleanup(void);
+extern int board_dfu_cleanup(void);
+extern int usb_gadget_handle_interrupts(void);
+
+#endif
include/flash_entity.h new file +39 -0
new file mode 100644
index 0000000..daa90ee
@@ -0,0 +1,39 @@
+/*
+ * flash_entity.h - flashable area description
+ *
+ * Copyright (C) 2011 Samsung Electronics
+ * author: Andrzej Pietrasiewicz <[email protected]>
+ *
+ * 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
+ */
+
+#ifndef FLASH_ENTITY_H_
+#define FLASH_ENTITY_H_
+
+#define FLASH_READ			0
+#define FLASH_WRITE			1
+
+struct flash_entity {
+	char				*name;
+	void				*ctx;
+	int (*prepare)(void *ctx, u8 mode);
+	int (*read_block)(void *ctx, unsigned int n, void *buf);
+	int (*write_block)(void *ctx, unsigned int n, void *buf);
+	int (*finish)(void *ctx, u8 mode);
+};
+
+void register_flash_entities(struct flash_entity *flents, int n);
+
+#endif /* FLASH_ENTITY_H_ */
include/mbr.h new file +49 -0

49 lines not on this page · raw message

Signature · 1 line
1.7.0.4
#1 lore raw

PATCH 1/1 dfu: initial implementation

Hello.

This really comes as surprise to me as I'm working on exactly the same at the moment. I just realised that I only mentioned it here inside the fastboot patches thread.

I started from the old patches Harald Welte wrote for u-boot during his work for OpenMoko. I worked with him during this time at OpenMoko and I'm the current maintainer of dfu-util. (Just as background).

You seem to have started from scratch with this DFU implementation instead of using the older but available patches. Was there a specific reason for this?

I found it not that complicated to update them on a current u-boot and adjust it accordingly. I have it working here with a beagleboard. Was about to send them out next week but you have been a small tick faster here. :)

#2 lore raw

PATCH 1/1 dfu: initial implementation

Hello Stefan,

Quoted 3 lines · Stefan Shmidt

Have you fully implemented 1.1? With the detahc logic inside the device implementation?

As you noticed in another post, the state machine is reused.

Quoted 2 lines

Just curious. What version of dfu-util your are using for your tests? I released 0.5 earlier today.

I used what was available in my desktop's Ubuntu 10.04 LTS, and it happens to be version 0.1+svn :O It works, though.

Quoted 2 lines

Hmm, that sounds like you only implemented the DFU mode but not the run-time mode yet?

This conclusion is not correct. After issuing the dfu command my implementation begins in runtime mode. Only after using dfu-util from the host is the mode changed to DFU mode.
Actually, as far as usage of dfu-util is concerned, I find it inconvenient when the device is in runtime mode, because there (at least with my version of dfu-util) is no way for the user to guess what altsettings are actually available. I personally do dfu-util -U <file> -a 0 or a similar command to kick the device into DFU mode, then dfu-util -l to discover what altsettings there are. So, it could be tempting to omit the runtime mode at all and start in DFU mode, but, to be standard compliant, I start in runtime mode. How do you discover with dfu-util what altsettings there are in the device?

Quoted 3 lines

While the original purpose
of DFU was to make updating USB device easy enough for end-users as well.

True, that's what the standard says about the very purpose of DFU. However, pressing keys to bring the device into a specific mode is probably very much device-specific. It would be nice if we are able to come up with a generic solution which only delegates the necessary details to board-specific files.

Quoted 4 lines

In the end I would like to see different options available:

1) Entering DFU mode directly when a button is pressed on startup. An update mode if you want to call it like this.

Please see my comment above.

Quoted 6 lines

2) Normal startup, but with DFU descripton to allow dfu-util detaching and entering the DFU mode for flashing.

3) Starting dfu mode form the command line as you did implement it.

I think 2 and 3 don't differ too much - it seems that enriching a setup without DFU with additional USB descriptors will do the job. But 2 and 3 probably should not be merged, though.

Quoted 8 lines
Quoted 1 line

The implementation is split into two parts: the dfu gadget

implementation

Quoted 3 lines

and a "flashing backend", the interface between the two being the struct flash_entity. The "flashing backend"'s implementation is board-specific and is implemented on the Goni target.

What is your reasoning behind this split? I have it working here with the normal nand_flashing routines taking care of bad blocks, etc.

The reason is to factor out board-specific stuff from the dfu gadget implementation. However, in what I factored out probably there are parts which are generic enough not to be board-specific. Since there seems to be considerable interest in DFU implementation for u-boot, this could be reworked.

Quoted 1 line

To make it clear, I'm happy to see somebody else working on this

"Whassssuuuup! --- Oh, man, we are not alone!" ;) I am happy to hear from someone else who works on this, too.

Regards,

Andrzej

#3 lore raw

PATCH 1/1 dfu: initial implementation

Hello.

Quoted 6 lines · Andrzej Pietrasiewicz
Quoted 3 lines · Stefan Shmidt

Have you fully implemented 1.1? With the detahc logic inside the device implementation?

As you noticed in another post, the state machine is reused.

Yes, sorry about this. I was fouled into thinking you did not as it was splitted out and no mentioning of Harald copyright. Having the same state machine is a good base for further cooperation on this. :)

Quoted 5 lines
Quoted 2 lines

Just curious. What version of dfu-util your are using for your tests? I released 0.5 earlier today.

I used what was available in my desktop's Ubuntu 10.04 LTS, and it happens to be version 0.1+svn :O It works, though.

Ah, latest Ubuntu has 0.3 and hopefully they will update as well. Already pinged the maintainer.

Quoted 4 lines
Quoted 2 lines

Hmm, that sounds like you only implemented the DFU mode but not the run-time mode yet?

This conclusion is not correct.

Indeed. I stand corrected.

Quoted 7 lines

After issuing the dfu command
my implementation begins in runtime mode. Only after using dfu-util from the host is the mode changed to DFU mode.
Actually, as far as usage of dfu-util is concerned, I find it inconvenient when the device is in runtime mode, because there (at least with my version of dfu-util) is no way for the user to guess what altsettings are actually available.

Correct. Sadly this is due to the standard only allowing the functional descriptor in runtime mode. On the other hand the exposed alt settings in run-time mode would clutter the USB device settings.

Quoted 3 lines

I personally
do dfu-util -U <file> -a 0 or a similar command to kick the device into DFU mode, then dfu-util -l to discover what altsettings there are.

I did run exactly in the same problem. :) I added a detach command to dfu-util to switch between the different modes. From run-time to dfu and from dfu to run-time. I wanted to get the release out before bringing it in. Pushed it to the master branch of dfu-util now. Feel free to test and let me know if it works for you.

Quoted 4 lines

So, it could be tempting to omit the runtime mode at all and start in DFU mode, but, to be standard compliant, I start in runtime mode. How do you discover with dfu-util what altsettings there are in the device?

See above. Not available in run-time mode. :/

Quoted 9 lines
Quoted 3 lines

While the original purpose
of DFU was to make updating USB device easy enough for end-users as well.

True, that's what the standard says about the very purpose of DFU. However, pressing keys to bring the device into a specific mode is probably very much device-specific. It would be nice if we are able to come up with a generic solution which only delegates the necessary details to board-specific files.

Agreed. This should not be handled in the dfu implementation at all. Your split between dfu and flashing backend is a good step in the right direction here. Invoking the dfu mode through a command is also a nice addition.

Quoted 6 lines
Quoted 6 lines
Quoted 1 line

The implementation is split into two parts: the dfu gadget

implementation

Quoted 3 lines

and a "flashing backend", the interface between the two being the struct flash_entity. The "flashing backend"'s implementation is board-specific and is implemented on the Goni target.

Quoted 8 lines
Quoted 2 lines

What is your reasoning behind this split? I have it working here with the normal nand_flashing routines taking care of bad blocks, etc.

The reason is to factor out board-specific stuff from the dfu gadget implementation. However, in what I factored out probably there are parts which are generic enough not to be board-specific. Since there seems to be considerable interest in DFU implementation for u-boot, this could be reworked.

After thinking more about it I agree with you here. The split makes sense and it will allow for more flexibility when adding more flashing backend to the implementation.

Quoted 4 lines
Quoted 1 line

To make it clear, I'm happy to see somebody else working on this

"Whassssuuuup! --- Oh, man, we are not alone!" ;) I am happy to hear from someone else who works on this, too.

Good to know. :)

Brainstorming about a way going forward. Please comment if with your view on this.

o I will send out my not ready for mainline patch to give you and others an impression how it is tackled in my patch.

o I like your split between dfu and flashing and also the addition of the dfu command. Could you split this part from the rest and send it as self conatining patch without other dependencies?

o And then one patch with the mmc/fat flashing backend in moved out of the board file (no idea what the best place is, maybe just another file for now) and a last patch with all the board specific changes?

o I will then rebase my code on yours and prepare patches against the dfu implementations as needed and also flashing backend for NAND.

o After this we need to review was is missing and bring it over.

Not all details covered in the plan but a start. Comments?

regards
Stefan Schmidt

#4 lore raw

PATCH 1/1 dfu: initial implementation

Hello,

Quoted 11 lines · Stefan Schmidt

o I will send out my not ready for mainline patch to give you and others an impression how it is tackled in my patch.

o I like your split between dfu and flashing and also the addition of the dfu command. Could you split this part from the rest and send it as self conatining patch without other dependencies?

o And then one patch with the mmc/fat flashing backend in moved out of the board file (no idea what the best place is, maybe just another file for now) and a last patch with all the board specific changes?

I assume splitting into 4 parts: (1) dfu gadget, (2) board specific code,
(3) flashing backend generic code, (4) dfu command.

As I today discovered, after latest merges in u-boot master the mmc subsystem in Goni does not work properly - the detected capacity is 0, and the number of available partitions is 0.
This took me some time to discover. I hope the responsible person will fix this soon. So, as far as the splitting of my dfu implementation into separate patches is concerned, I think I will come up with it early next week.

Quoted 7 lines

o I will then rebase my code on yours and prepare patches against the dfu implementations as needed and also flashing backend for NAND.

o After this we need to review was is missing and bring it over.

Not all details covered in the plan but a start. Comments?

In the beginning I intend to split the code into respective parts, to give you something to work with. Then I intend to clean the code up, taking into account your and Mike's review.

Regards,

Andrzej

#5 lore raw

PATCH 1/1 dfu: initial implementation

In reply to Andrzej Pietrasiewicz
Quoted 8 lines · On Wednesday 02 November 2011 06:00:07 Andrzej Pietrasiewicz wrote:

---

Dear All,

This is Device Firmware Upgrade (DFU) implementation which supports data upload and download function to devices which are equipped with a UDC.

this information belongs in the changelog (above the "---" marker)

are you working with the elinux.org guys ?
http://elinux.org/Merge_DFU_support_into_mainline_U-Boot

Quoted 5 lines
 board/samsung/goni/Makefile |    2 +
 board/samsung/goni/flash.c  |  634
 board/samsung/goni/flash.h  |   28 ++
 board/samsung/goni/goni.c   |   17 +
 common/Makefile             |    1 +
Quoted 8 lines
 common/cmd_dfu.c            |   50 +++
 drivers/usb/gadget/Makefile |    1 +
 drivers/usb/gadget/dfu.c    |  920
 drivers/usb/gadget/dfu.h    |  171
 include/configs/s5p_goni.h  |    6 +
 include/dfu.h               |   31 ++
 include/flash_entity.h      |   39 ++
 include/mbr.h               |   49 +++

this should be split up into at least the dfu core and board-specific changes. although i'd wonder how much of the board/samsung/ stuff is really board specific and couldn't be generalized ...

include/dfu.h

Quoted 4 lines
include/dfu.h new file +1 -0
 
+extern int usb_gadget_handle_interrupts(void);

this doesn't belong in the dfu header

include/flash_entity.h line 29

Quoted 5 lines
include/flash_entity.h new file +2 -0
 
+struct flash_entity {
+	char				*name;

should probably be const

Quoted 13 lines
include/mbr.h new file +10 -0
 
+/*
+ * Copyright (C) 2010 Samsung Electrnoics
+ *
+ * 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

include/mbr.h line 20

Quoted 10 lines
include/mbr.h new file +10 -0
+ * 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
+ */
+
+#include <linux/compiler.h>

missing ifdef protection against multiple inclusion

this also should be split out of the dfu core ... although it seems weird that u-boot doesn't already have mbr parsing code ...
-mike
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 836 bytes
Desc: This is a digitally signed message part.
Url : http://lists.denx.de/pipermail/u-boot/attachments/20111102/11ca9ca8/attachment.pgp

#6 lore raw

PATCH 1/1 dfu: initial implementation

Hello.

Quoted 4 lines · Mike Frysinger

On Wednesday 02 November 2011 06:00:07 Andrzej Pietrasiewicz wrote:

are you working with the elinux.org guys ?
http://elinux.org/Merge_DFU_support_into_mainline_U-Boot

That would be me. As I stated in my other mail I was surprised seeing this patch coming in. I did not communicate my work on this broadly either. Wanted to show up with soem real code not only promises. Same as Andrzej it seems. :)

A quick comparison shows me that there are areas that don't touch each other at all (mmc and images on fat as backend vs. raw nand flash as backend) and areas that are duplicated and needed to get sorted out. The DFU protocol implementation in this case. I hope Andrzej is willing to work toegtehr with me on a final implementation that contains pieces of both versions. Mine is missing a code split between DFU protocol and flashing backend for example but feature other parts missing here.

Quoted 14 lines
Quoted 5 lines
 board/samsung/goni/Makefile |    2 +
 board/samsung/goni/flash.c  |  634
 board/samsung/goni/flash.h  |   28 ++
 board/samsung/goni/goni.c   |   17 +
 common/Makefile             |    1 +
Quoted 8 lines
 common/cmd_dfu.c            |   50 +++
 drivers/usb/gadget/Makefile |    1 +
 drivers/usb/gadget/dfu.c    |  920
 drivers/usb/gadget/dfu.h    |  171
 include/configs/s5p_goni.h  |    6 +
 include/dfu.h               |   31 ++
 include/flash_entity.h      |   39 ++
 include/mbr.h               |   49 +++
Quoted 3 lines

this should be split up into at least the dfu core and board-specific changes. although i'd wonder how much of the board/samsung/ stuff is really board specific and couldn't be generalized ...

Agreed. The eMMC flashing with files on FAT is nothing goni specific. Others should be able to use this as well. I see three different parts here that can be separated:

1) The DFU protocol implementation which lives in usb gadget with an interface for flashing backends

2) The flashing backends (no idea where to put them best). At the moment that would be the implementation here with eMMC and files on FAT, mine with raw NAND devices and in the future maybe others. Each flashing backend should be generic enough to allow different boards using it.

3) Board specific information like partitions or hints for the flashing backend. The information itself should be in the board file here and only used by the flashing backends.

How does this sound to you? Andrzej?

regards
Stefan Schmidt
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 250 bytes
Desc: Digital signature
Url : http://lists.denx.de/pipermail/u-boot/attachments/20111102/4d516221/attachment.pgp

#7 lore raw

PATCH 1/1 dfu: initial implementation

Hello Stefan,

Quoted 4 lines · Stefan Schmidt

Agreed. The eMMC flashing with files on FAT is nothing goni specific. Others should be able to use this as well. I see three different parts here that can be separated:

I agree. Since there is interest in DFU implementation this can and should be generalized.

Quoted 10 lines

1) The DFU protocol implementation which lives in usb gadget with an interface for flashing backends

2) The flashing backends (no idea where to put them best). At the moment that would be the implementation here with eMMC and files on FAT, mine with raw NAND devices and in the future maybe others. Each flashing backend should be generic enough to allow different boards using it.

Quoted 5 lines

3) Board specific information like partitions or hints for the flashing backend. The information itself should be in the board file here and only used by the flashing backends.

How does this sound to you? Andrzej?

Sounds good to me. In my implementation the interface between dfu gadget and flashing backend is around the struct flash_entity.
It contains a character string intended to provide a human-readable name, a void * context which is not interpreted by the gadget code, but is passed to the flashing backend and understood by it. The struct flash_entity also contains prepare-finish methods to be called before and after read/write operations, and the read_block- write_block pair. What do you think?

As far as generalization is concerned, in my flashing backend implementation I see these parts as candidates for generalization:

1) mbr/ebr reading
2) reading/writing mmc
3) read/write fat
4) generic prepare/finish; not sure if fat-specific prepare can be generalized
5) read/write_block
6) some more work should be put to create an interface between the board initialization routine, the flashing backend and the DFU gadget implementation.
In my implementation the board initialization routine calls board-specific register_flash_areas, which, in turn, calls DFU gadget's register_flash_entities.
What's your opinion?

Thanks,

Andrzej

#8 lore raw

PATCH 1/1 dfu: initial implementation

Hello.

Quoted 8 lines · Andrzej Pietrasiewicz
Quoted 4 lines · Stefan Schmidt

Agreed. The eMMC flashing with files on FAT is nothing goni specific. Others should be able to use this as well. I see three different parts here that can be separated:

I agree. Since there is interest in DFU implementation this can and should be generalized.

Great!

Quoted 15 lines
Quoted 9 lines

1) The DFU protocol implementation which lives in usb gadget with an interface for flashing backends

2) The flashing backends (no idea where to put them best). At the moment that would be the implementation here with eMMC and files on FAT, mine with raw NAND devices and in the future maybe others. Each flashing backend should be generic enough to allow different boards using it.

Quoted 5 lines

3) Board specific information like partitions or hints for the flashing backend. The information itself should be in the board file here and only used by the flashing backends.

How does this sound to you? Andrzej?

Quoted 8 lines

Sounds good to me. In my implementation the interface between dfu gadget and flashing backend is around the struct flash_entity.
It contains a character string intended to provide a human-readable name, a void * context which is not interpreted by the gadget code, but is passed to the flashing backend and understood by it. The struct flash_entity also contains prepare-finish methods to be called before and after read/write operations, and the read_block- write_block pair. What do you think?

I would need to use it for my NAND backend before I really can comment on it. I can only tell if I'm happy with and interface if I actually used it. :)

Will do this when you send it a separate patch with only the DFU implementation with the flash entity as interface. See my roadmap proposal in the other mail.

Quoted 6 lines

As far as generalization is concerned, in my flashing backend implementation I see these parts as candidates for generalization:

1) mbr/ebr reading
2) reading/writing mmc
3) read/write fat

Agreed. That can be used by all kind of devices.

Quoted 2 lines

4) generic prepare/finish; not sure if fat-specific prepare can be generalized

Would be good to get soem comments on the fs custodians around here.

Quoted 1 line

5) read/write_block

Agreed.

Quoted 7 lines

6) some more work should be put to create an interface between the board initialization routine, the flashing backend and the DFU gadget implementation.
In my implementation the board initialization routine calls board-specific register_flash_areas, which, in turn, calls DFU gadget's register_flash_entities.
What's your opinion?

The interface between the configuration in the board file and the actual flashing backend is something I haven't wraped my mind around yet. Need to think about it.

regards
Stefan Schmidt

#9 lore raw

PATCH 1/1 dfu: initial implementation

In reply to Mike Frysinger

Hello Mike,

Thank you for your review. Please see my comments inline.

Quoted 9 lines · Mike Frysinger
Quoted 5 lines

Dear All,

This is Device Firmware Upgrade (DFU) implementation which supports data upload and download function to devices which are equipped with

a UDC.

this information belongs in the changelog (above the "---" marker)

I generally agree to remarks related to coding style and implementation's distribution into respective patches.

Quoted 3 lines

are you working with the elinux.org guys ?
http://elinux.org/Merge_DFU_support_into_mainline_U-Boot

That's not me.

Quoted 5 lines

this should be split up into at least the dfu core and board-specific changes.
although i'd wonder how much of the board/samsung/ stuff is really board specific and couldn't be generalized ...

You are right, probably the "flashing backend" part contains some code which can be generalized.

Regards,

Andrzej

#10 lore raw

PATCH 1/1 dfu: initial implementation

In reply to Andrzej Pietrasiewicz

Hello.

@Remy: One question I have for you is if the DFU implementation should be based on the re-written gadget layer from samsung or based on the current one?

First, and only high level, review for the DFU part.

Against which u-boot tree/branch/version is this patch? I tried to apply it against HEAD and it failed for me. Anything I miss?

Quoted 5 lines · Andrzej Pietrasiewicz

The implementation is split into two parts: the dfu gadget implementation and a "flashing backend", the interface between the two being the struct flash_entity. The "flashing backend"'s implementation is board-specific and is implemented on the Goni target.

I replied to Mike's mail with my ideas on this. Split between dfu and flashing backends is good and missng in my approach currently. I would not put it into the board file though but make it generic for other boards as well and only define the needed informations in the board file. Please see my other mail for details. Comments appreciated!

common/Makefile COBJS-y += cmd_usb.o · line 165

Quoted 9 lines
common/Makefile +1 -0
index ae795e0..de926d9 100644
@@ -162,6 +162,7 @@ COBJS-y += cmd_usb.o
 COBJS-y += usb.o
 COBJS-$(CONFIG_USB_STORAGE) += usb_storage.o
 endif
+COBJS-$(CONFIG_CMD_DEVICE_FIRMWARE_UPGRADE) += cmd_dfu.o

CONFIG_CMD_DFU instead? Looks a bit long to me.

common/cmd_dfu.c

Quoted 4 lines
common/Makefile +0 -0
 COBJS-$(CONFIG_CMD_XIMG) += cmd_ximg.o
 COBJS-$(CONFIG_YAFFS2) += cmd_yaffs2.o
 
common/cmd_dfu.c +0 -0

V> new file mode 100644

Quoted 44 lines

index 0000000..c85fbb1

common/cmd_dfu.c new file +40 -0
@@ -0,0 +1,50 @@
+/*
+ * Copyright (C) 2011 Samsung Electrnoics
+ * author: Andrzej Pietrasiewicz <[email protected]>
+ *
+ * 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
+ */
+
+#include <common.h>
+#include <command.h>
+#include <dfu.h>
+#include <flash_entity.h>
+
+static int do_dfu(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
+{
+	board_dfu_init();
+	dfu_init();
+	while (1) {
+		int irq_res;
+		/* Handle control-c and timeouts */
+		if (ctrlc()) {
+			printf("The remote end did not respond in time.\n");
+			goto fail;
+		}
+
+		irq_res = usb_gadget_handle_interrupts();
+	}
+fail:

common/cmd_dfu.c line 50

Quoted 10 lines
common/cmd_dfu.c new file +10 -0
+	dfu_cleanup();
+	board_dfu_cleanup();
+	return -1;
+}
+
+U_BOOT_CMD(dfu, CONFIG_SYS_MAXARGS, 1, do_dfu,
+	"Use the DFU [Device Firmware Upgrade]",
+	"dfu - device firmware upgrade"
+);
+

While I think a dfu command is usefull I don't like the need to execute it before any DFU interaction can happen. That may be an option during development but for field upgrades or receovery it is not.

I already wrote a bit about my approach here. At OpenMoko we used a button to enter u-boot during startup when pressed. This offered a usbtty interface as usb gadget as well as the runtime descripto for DFU. With dfu-util it was possible to iniate the DFU download or upload procedure while being in the mode. Another option would be to directly jump into DFU mode when a button is pressed.

Quoted 41 lines
drivers/usb/gadget/dfu.c new file +35 -0
new file mode 100644
index 0000000..535e194
@@ -0,0 +1,920 @@
+/*
+ * dfu.c -- Device Firmware Update gadget
+ *
+ * Copyright (C) 2011 Samsung Electronics
+ * author: Andrzej Pietrasiewicz <[email protected]>
+ *
+ * Based on gadget zero:
+ * Copyright (C) 2003-2007 David Brownell
+ * All rights reserved.
+ *
+ * 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
+ */
+
+#define VERBOSE_DEBUG
+#define DEBUG
+
+/*
+#include <linux/kernel.h>
+#include <linux/utsname.h>
+#include <linux/device.h>
+*/
+
+#include <common.h>

drivers/usb/gadget/dfu.c line 45

Quoted 10 lines
drivers/usb/gadget/dfu.c new file +10 -0
+#include <asm-generic/errno.h>
+#include <linux/usb/ch9.h>
+#include <linux/usb/gadget.h>
+
+#include <flash_entity.h>
+
+#include "gadget_chips.h"
+/* #include "epautoconf.c" */
+/* #include "config.c" */
+/* #include "usbstring.c" */

Not needed, can be removed.

Quoted 30 lines
drivers/usb/gadget/dfu.c new file +30 -0
+#include <malloc.h>
+#include "dfu.h"
+
+static struct flash_entity *flash_ents;
+static int num_flash_ents;
+
+static struct usb_device_descriptor device_desc = {
+	.bLength =		sizeof device_desc,
+	.bDescriptorType =	USB_DT_DEVICE,
+	.bcdUSB =		__constant_cpu_to_le16(0x0100),
+	.bDeviceClass =		USB_CLASS_VENDOR_SPEC,
+	.idVendor =		__constant_cpu_to_le16(DRIVER_VENDOR_NUM),
+	.idProduct =		__constant_cpu_to_le16(DRIVER_PRODUCT_NUM),
+	.iManufacturer =	STRING_MANUFACTURER,
+	.iProduct =		STRING_PRODUCT,
+	.iSerialNumber =	STRING_SERIAL,
+	.bNumConfigurations =	1,
+};
+
+static struct usb_config_descriptor dfu_config = {
+	.bLength =		sizeof dfu_config,
+	.bDescriptorType =	USB_DT_CONFIG,
+	/* compute wTotalLength on the fly */
+	.bNumInterfaces =	1,
+	.bConfigurationValue =	DFU_CONFIG_VAL,
+	.iConfiguration =	STRING_DFU_NAME,
+	.bmAttributes =		USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
+	.bMaxPower =		1,	/* self-powered */
+};
+

drivers/usb/gadget/dfu.c line 86

Quoted 10 lines
drivers/usb/gadget/dfu.c new file +10 -0
+static struct usb_otg_descriptor otg_descriptor = {
+	.bLength =		sizeof otg_descriptor,
+	.bDescriptorType =	USB_DT_OTG,
+	.bmAttributes =		USB_OTG_SRP,
+};
+
+static const struct dfu_function_descriptor dfu_func = {
+	.bLength =		sizeof dfu_func,
+	.bDescriptorType =	DFU_DT_FUNC,
+	.bmAttributes =		DFU_BIT_WILL_DETACH |

Here are you setting the WILL_DETACH bit. Is the device really detaching itself? Its not obvious to me from the code that it does.

Quoted 29 lines
drivers/usb/gadget/dfu.c new file +29 -0
+				DFU_BIT_MANIFESTATION_TOLERANT |
+				DFU_BIT_CAN_UPLOAD |
+				DFU_BIT_CAN_DNLOAD,
+	.wDetachTimeOut =	0,
+	.wTransferSize =	USB_BUFSIZ,
+	.bcdDFUVersion =	__constant_cpu_to_le16(0x0110),
+};
+
+static const struct usb_interface_descriptor dfu_intf_runtime = {
+	.bLength =		sizeof dfu_intf_runtime,
+	.bDescriptorType =	USB_DT_INTERFACE,
+	.bNumEndpoints =	0,
+	.bInterfaceClass =	USB_CLASS_APP_SPEC,
+	.bInterfaceSubClass =	1,
+	.bInterfaceProtocol =	1,
+	.iInterface =		STRING_DFU_NAME,
+};
+
+static const struct usb_descriptor_header *dfu_function_runtime[] = {
+	(struct usb_descriptor_header *) &otg_descriptor,
+	(struct usb_descriptor_header *) &dfu_func,
+	(struct usb_descriptor_header *) &dfu_intf_runtime,
+	NULL,
+};
+
+static struct usb_qualifier_descriptor dev_qualifier = {
+	.bLength =		sizeof dev_qualifier,
+	.bDescriptorType =	USB_DT_DEVICE_QUALIFIER,
+	.bcdUSB =		__constant_cpu_to_le16(0x0200),

drivers/usb/gadget/dfu.c line 125

Quoted 10 lines
drivers/usb/gadget/dfu.c new file +10 -0
+	.bDeviceClass =		USB_CLASS_VENDOR_SPEC,
+	.bNumConfigurations =	1,
+};
+
+static char manufacturer[50];
+static const char longname[] = "DFU Gadget";
+/* default serial number takes at least two packets */
+static const char serial[] = "0123456789.0123456789.012345678";
+static const char dfu_name[] = "Device Firmware Upgrade";
+static const char shortname[] = "dfu";

This surely should be come from the board configuration?

Quoted 14 lines
drivers/usb/gadget/dfu.c new file +14 -0
+
+/* static strings, in UTF-8 */
+static struct usb_string strings_runtime[] = {
+	{ STRING_MANUFACTURER, manufacturer, },
+	{ STRING_PRODUCT, longname, },
+	{ STRING_SERIAL, serial, },
+	{ STRING_DFU_NAME, dfu_name, },
+	{  }			/* end of list */
+};
+
+static struct usb_gadget_strings stringtab_runtime = {
+	.language	= 0x0409,	/* en-us */
+	.strings	= strings_runtime,
+};

drivers/usb/gadget/dfu.c line 149

Quoted 10 lines
drivers/usb/gadget/dfu.c new file +10 -0
+
+static struct usb_gadget_strings stringtab_dfu = {
+	.language	= 0x0409,	/* en-us */
+};
+
+static bool is_runtime(struct dfu_dev *dev)
+{
+	return dev->dfu_state == DFU_STATE_appIDLE ||
+		dev->dfu_state == DFU_STATE_appDETACH;
+}

Hmm, here you are checking if you are in run-time mode. In the introduction you wrote that it is in DFU mode when the dfu command is executed. When does it enter the run-time mode? After the first flashing?

Quoted 114 lines
drivers/usb/gadget/dfu.c new file +114 -0
+static int config_buf(struct usb_gadget *gadget,
+		u8 *buf, u8 type, unsigned index)
+{
+	int hs = 0;
+	struct dfu_dev *dev = get_gadget_data(gadget);
+	const struct usb_descriptor_header **function;
+	int len;
+
+	if (index > 0)
+		return -EINVAL;
+
+	if (gadget_is_dualspeed(gadget)) {
+		hs = (gadget->speed == USB_SPEED_HIGH);
+		if (type == USB_DT_OTHER_SPEED_CONFIG)
+			hs = !hs;
+	}
+	if (is_runtime(dev))
+		function = dfu_function_runtime;
+	else
+		function = (const struct usb_descriptor_header **)dev->function;
+
+	/* for now, don't advertise srp-only devices */
+	if (!gadget_is_otg(gadget))
+		function++;
+
+	len = usb_gadget_config_buf(&dfu_config,
+			buf, USB_BUFSIZ, function);
+	if (len < 0)
+		return len;
+	((struct usb_config_descriptor *) buf)->bDescriptorType = type;
+	return len;
+}
+
+static void dfu_reset_config(struct dfu_dev *dev)
+{
+	if (dev->config == 0)
+		return;
+
+	DBG(dev, "reset config\n");
+
+	dev->config = 0;
+}
+
+static int dfu_set_config(struct dfu_dev *dev, unsigned number)
+{
+	int result = 0;
+	struct usb_gadget *gadget = dev->gadget;
+	char *speed;
+
+	if (number == dev->config)
+		return 0;
+
+	dfu_reset_config(dev);
+
+	if (DFU_CONFIG_VAL != number) {
+		result = -EINVAL;
+		return result;
+	}
+
+	switch (gadget->speed) {
+	case USB_SPEED_LOW:
+		speed = "low";
+		break;
+	case USB_SPEED_FULL:
+		speed = "full";
+		break;
+	case USB_SPEED_HIGH:
+		speed = "high";
+		break;
+	default:
+		speed = "?";
+		break;
+	}
+
+	dev->config = number;
+	INFO(dev, "%s speed config #%d: %s\n", speed, number, dfu_name);
+	return result;
+}
+
+/*-------------------------------------------------------------------------*/
+
+static void empty_complete(struct usb_ep *ep, struct usb_request *req)
+{
+	/* intentionally empty */
+}
+
+static void dnload_request_complete(struct usb_ep *ep, struct usb_request *req)
+{
+	struct dfu_dev *dev = req->context;
+	struct flash_entity *fe = &flash_ents[dev->altsetting];
+
+	if (dev->not_prepared) {
+		printf("DOWNLOAD %s\n", fe->name);
+		fe->prepare(fe->ctx, FLASH_WRITE);
+		dev->not_prepared = false;
+	}
+
+	if (req->length > 0)
+		fe->write_block(fe->ctx, req->length, req->buf);
+	else {
+		fe->finish(fe->ctx, FLASH_WRITE);
+		dev->not_prepared = true;
+	}
+}
+
+static void handle_getstatus(struct usb_request *req)
+{
+	struct dfu_status *dstat = (struct dfu_status *)req->buf;
+	struct dfu_dev *dev = req->context;
+
+	switch (dev->dfu_state) {
+	case DFU_STATE_dfuDNLOAD_SYNC:
+	case DFU_STATE_dfuDNBUSY:
+		dev->dfu_state = DFU_STATE_dfuDNLOAD_IDLE;

drivers/usb/gadget/dfu.c line 274

Quoted 10 lines
drivers/usb/gadget/dfu.c new file +10 -0
+		break;
+	case DFU_STATE_dfuMANIFEST_SYNC:
+		break;
+	default:
+		break;
+	}
+
+	/* send status response */
+	dstat->bStatus = dev->dfu_status;
+	/* FIXME: set dstat->bwPollTimeout */

This really needs to be set. It was a nightmare with dfu-util not having it set with the initial u-boot implementation. setting the correct values here is not that easy though. It depends on the flash layer and if can get the information about the maximal time a flash write can take.

Quoted 105 lines
drivers/usb/gadget/dfu.c new file +105 -0
+	dstat->bState = dev->dfu_state;
+	dstat->iString = 0;
+}
+
+static void handle_getstate(struct usb_request *req)
+{
+	struct dfu_dev *dev = req->context;
+
+	((u8 *)req->buf)[0] = dev->dfu_state & 0xff;
+	req->actual = sizeof(u8);
+}
+
+static int handle_upload(struct usb_request *req, u16 len)
+{
+	struct dfu_dev *dev = req->context;
+	struct flash_entity *fe = &flash_ents[dev->altsetting];
+	int n;
+
+	if (dev->not_prepared) {
+		printf("UPLOAD %s\n", fe->name);
+		fe->prepare(fe->ctx, FLASH_READ);
+		dev->not_prepared = false;
+	}
+	n = fe->read_block(fe->ctx, len, req->buf);
+
+	/* no more data to read from this entity */
+	if (n < len) {
+		fe->finish(fe->ctx, FLASH_READ);
+		dev->not_prepared = true;
+	}
+
+	return n;
+}
+
+static int handle_dnload(struct usb_gadget *gadget, u16 len)
+{
+	struct dfu_dev *dev = get_gadget_data(gadget);
+	struct usb_request *req = dev->req;
+
+	if (len == 0)
+		dev->dfu_state = DFU_STATE_dfuMANIFEST_SYNC;
+
+	req->complete = dnload_request_complete;
+
+	return len;
+}
+
+static int
+dfu_handle(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
+{
+	struct dfu_dev *dev = get_gadget_data(gadget);
+	struct usb_request *req = dev->req;
+	u16 len = le16_to_cpu(ctrl->wLength);
+	int rc = 0;
+
+	switch (dev->dfu_state) {
+	case DFU_STATE_appIDLE:
+		switch (ctrl->bRequest) {
+		case USB_REQ_DFU_GETSTATUS:
+			handle_getstatus(req);
+			return RET_STAT_LEN;
+		case USB_REQ_DFU_GETSTATE:
+			handle_getstate(req);
+			break;
+		case USB_REQ_DFU_DETACH:
+			dev->dfu_state = DFU_STATE_appDETACH;
+			dev->dfu_state = DFU_STATE_dfuIDLE;
+			return RET_ZLP;
+		default:
+			return RET_STALL;
+		}
+		break;
+	case DFU_STATE_appDETACH:
+		switch (ctrl->bRequest) {
+		case USB_REQ_DFU_GETSTATUS:
+			handle_getstatus(req);
+			return RET_STAT_LEN;
+		case USB_REQ_DFU_GETSTATE:
+			handle_getstate(req);
+			break;
+		default:
+			dev->dfu_state = DFU_STATE_appIDLE;
+			return RET_STALL;
+		}
+		/* FIXME: implement timer to return to appIDLE */
+		break;
+	case DFU_STATE_dfuIDLE:
+		switch (ctrl->bRequest) {
+		case USB_REQ_DFU_DNLOAD:
+			if (len == 0) {
+				dev->dfu_state = DFU_STATE_dfuERROR;
+				return RET_STALL;
+			}
+			dev->dfu_state = DFU_STATE_dfuDNLOAD_SYNC;
+			return handle_dnload(gadget, len);
+		case USB_REQ_DFU_UPLOAD:
+			dev->dfu_state = DFU_STATE_dfuUPLOAD_IDLE;
+			return handle_upload(req, len);
+		case USB_REQ_DFU_ABORT:
+			/* no zlp? */
+			return RET_ZLP;
+		case USB_REQ_DFU_GETSTATUS:
+			handle_getstatus(req);
+			return RET_STAT_LEN;
+		case USB_REQ_DFU_GETSTATE:

drivers/usb/gadget/dfu.c line 389

Quoted 10 lines
drivers/usb/gadget/dfu.c new file +10 -0
+			handle_getstate(req);
+			break;
+		case USB_REQ_DFU_DETACH:
+			/* Proprietary extension: 'detach' from idle mode and
+			 * get back to runtime mode in case of USB Reset.  As
+			 * much as I dislike this, we just can't use every USB
+			 * bus reset to switch back to runtime mode, since at
+			 * least the Linux USB stack likes to send a number of
+			 * resets in a row :(
+			 */

OK, this makes it clear that you took the DFU state machine from Haralds implementation I'm also using. :)

It would be nice if the related copyright holder would be stated in the header of the file.

Quoted 113 lines
drivers/usb/gadget/dfu.c new file +113 -0
+			dev->dfu_state = DFU_STATE_dfuMANIFEST_WAIT_RST;
+			dev->dfu_state = DFU_STATE_appIDLE;
+			break;
+		default:
+			dev->dfu_state = DFU_STATE_dfuERROR;
+			return RET_STALL;
+		}
+		break;
+	case DFU_STATE_dfuDNLOAD_SYNC:
+		switch (ctrl->bRequest) {
+		case USB_REQ_DFU_GETSTATUS:
+			handle_getstatus(req);
+			return RET_STAT_LEN;
+			/* FIXME: state transition depending
+			 * on block completeness */
+		case USB_REQ_DFU_GETSTATE:
+			handle_getstate(req);
+			break;
+		default:
+			dev->dfu_state = DFU_STATE_dfuERROR;
+			return RET_STALL;
+		}
+		break;
+	case DFU_STATE_dfuDNBUSY:
+		switch (ctrl->bRequest) {
+		case USB_REQ_DFU_GETSTATUS:
+			/* FIXME: only accept getstatus if bwPollTimeout
+			 * has elapsed */
+			handle_getstatus(req);
+			return RET_STAT_LEN;
+		default:
+			dev->dfu_state = DFU_STATE_dfuERROR;
+			return RET_STALL;
+		}
+		break;
+	case DFU_STATE_dfuDNLOAD_IDLE:
+		switch (ctrl->bRequest) {
+		case USB_REQ_DFU_DNLOAD:
+			dev->dfu_state = DFU_STATE_dfuDNLOAD_SYNC;
+			return handle_dnload(gadget, len);
+		case USB_REQ_DFU_ABORT:
+			dev->dfu_state = DFU_STATE_dfuIDLE;
+			return RET_ZLP;
+		case USB_REQ_DFU_GETSTATUS:
+			handle_getstatus(req);
+			return RET_STAT_LEN;
+		case USB_REQ_DFU_GETSTATE:
+			handle_getstate(req);
+			break;
+		default:
+			dev->dfu_state = DFU_STATE_dfuERROR;
+			return RET_STALL;
+		}
+		break;
+	case DFU_STATE_dfuMANIFEST_SYNC:
+		switch (ctrl->bRequest) {
+		case USB_REQ_DFU_GETSTATUS:
+			/* We're MainfestationTolerant */
+			dev->dfu_state = DFU_STATE_dfuIDLE;
+			handle_getstatus(req);
+			return RET_STAT_LEN;
+		case USB_REQ_DFU_GETSTATE:
+			handle_getstate(req);
+			break;
+		default:
+			dev->dfu_state = DFU_STATE_dfuERROR;
+			return RET_STALL;
+		}
+		break;
+	case DFU_STATE_dfuMANIFEST:
+		/* we should never go here */
+		dev->dfu_state = DFU_STATE_dfuERROR;
+		return RET_STALL;
+	case DFU_STATE_dfuMANIFEST_WAIT_RST:
+		/* we should never go here */
+		break;
+	case DFU_STATE_dfuUPLOAD_IDLE:
+		switch (ctrl->bRequest) {
+		case USB_REQ_DFU_UPLOAD:
+			/* state transition if less data then requested */
+			rc = handle_upload(req, len);
+			if (rc >= 0 && rc < len)
+				dev->dfu_state = DFU_STATE_dfuIDLE;
+			return rc;
+		case USB_REQ_DFU_ABORT:
+			dev->dfu_state = DFU_STATE_dfuIDLE;
+			/* no zlp? */
+			return RET_ZLP;
+		case USB_REQ_DFU_GETSTATUS:
+			handle_getstatus(req);
+			return RET_STAT_LEN;
+		case USB_REQ_DFU_GETSTATE:
+			handle_getstate(req);
+			break;
+		default:
+			dev->dfu_state = DFU_STATE_dfuERROR;
+			return RET_STALL;
+		}
+		break;
+	case DFU_STATE_dfuERROR:
+		switch (ctrl->bRequest) {
+		case USB_REQ_DFU_GETSTATUS:
+			handle_getstatus(req);
+			return RET_STAT_LEN;
+		case USB_REQ_DFU_GETSTATE:
+			handle_getstate(req);
+			break;
+		case USB_REQ_DFU_CLRSTATUS:
+			dev->dfu_state = DFU_STATE_dfuIDLE;
+			dev->dfu_status = DFU_STATUS_OK;
+			/* no zlp? */
+			return RET_ZLP;
+		default:

drivers/usb/gadget/dfu.c line 512

Quoted 10 lines
drivers/usb/gadget/dfu.c new file +10 -0
+			dev->dfu_state = DFU_STATE_dfuERROR;
+			return RET_STALL;
+		}
+		break;
+	default:
+		return -1;
+	}
+
+	return 0;
+}

[snap]

drivers/usb/gadget/dfu.c line 912

Quoted 4 lines
drivers/usb/gadget/dfu.c new file +4 -0
+int __init dfu_init(void)
+{
+	return usb_gadget_register_driver(&dfu_driver);
+}

All this is absed on the re-written gadget layer? It loks as if all kind of kernel stuff is brought over here. Is this gadget layer already merged into u-boot mainline?

Quoted 54 lines
drivers/usb/gadget/dfu.h new file +48 -0
new file mode 100644
index 0000000..5a6386e
@@ -0,0 +1,171 @@
+/*
+ * dfu.h -- Device Firmware Update gadget
+ *
+ * Copyright (C) 2011 Samsung Electronics
+ * author: Andrzej Pietrasiewicz <[email protected]>
+ *
+ * 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
+ */
+
+#ifndef DFU_H_
+#define DFU_H_
+
+#include <linux/compiler.h>
+
+/*
+ * Linux kernel compatibility layer
+ */
+#define GFP_ATOMIC				((gfp_t) 0)
+#define GFP_KERNEL				((gfp_t) 0)
+#define true					1
+#define false					0
+#define dev_dbg(...)				do {} while (0)
+#define dev_vdbg(...)				do {} while (0)
+#define dev_err(...)				do {} while (0)
+#define dev_warn(...)				do {} while (0)
+#define dev_info(...)				do {} while (0)
+#define pr_warning(...)				do {} while (0)
+#define spin_lock_init(lock)			do {} while (0)
+#define spin_lock(lock)				do {} while (0)
+#define spin_unlock(lock)			do {} while (0)
+#define spin_lock_irqsave(lock,flags)		do {flags = 1;} while (0)
+#define spin_unlock_irqrestore(lock,flags)	do {flags = 0;} while (0)
+#define kmalloc(x,y)				malloc(x)
+#define kfree(x)				free(x)
+#define kzalloc(size,flags)			calloc((size), 1)
+#define __init

drivers/usb/gadget/dfu.h line 58

Quoted 10 lines
drivers/usb/gadget/dfu.h new file +10 -0
+#define __exit
+#define __exit_p(x)				x
+#define module_init(...)
+#define module_exit(...)
+#define min_t					min
+#define spinlock_t				int
+#define bool					int
+/*
+ * end compatibility layer
+ */

Urgs, is this all needed? Do we really want to have all this around. This is not the kernel. U-Boots debug infrastrcuture should be used.

drivers/usb/gadget/dfu.h line 71

Quoted 12 lines
drivers/usb/gadget/dfu.h new file +12 -0
+#define DBG(d, fmt, args...) \
+	dev_dbg(&(d)->gadget->dev , fmt , ## args)
+#define VDBG(d, fmt, args...) \
+	dev_vdbg(&(d)->gadget->dev , fmt , ## args)
+#define ERROR(d, fmt, args...) \
+	dev_err(&(d)->gadget->dev , fmt , ## args)
+#define WARN(d, fmt, args...) \
+	dev_warn(&(d)->gadget->dev , fmt , ## args)
+#define INFO(d, fmt, args...) \
+	dev_info(&(d)->gadget->dev , fmt , ## args)
+
+#define DRIVER_VERSION			"Marzanna"

Maybe that helps you internal but it does not make much sense for people outside Samsung. :)

It was good to see that the DFU state machine was taken from Haralds patches. That should make it a lot easier to find a common ground between the implementations. And I agree that splitting out the DFU protocol from the flashing parts makes sense.

I would like to get some initila feedback from Remy if this should be based on the new gadget stuff or not.

How would you like to proceed in getting our stuff merged and finally integrated into mainline?

regards
Stefan Schmidt

#11 lore raw

PATCH 1/1 dfu: initial implementation

Hello Stefan,

Thank you for your review. Please see comments inline.

Quoted 6 lines · Stefan Schmidt

First, and only high level, review for the DFU part.

Against which u-boot tree/branch/version is this patch? I tried to apply it against HEAD and it failed for me. Anything I miss?

Sorry about that. I forgot to mention the reference. It is
http://patchwork.ozlabs.org/patch/122080

Quoted 8 lines · Andrzej Pietrasiewicz
Quoted 2 lines

The implementation is split into two parts: the dfu gadget

implementation

Quoted 3 lines

and a "flashing backend", the interface between the two being the struct flash_entity. The "flashing backend"'s implementation is board-specific and is implemented on the Goni target.

Quoted 5 lines

I replied to Mike's mail with my ideas on this. Split between dfu and flashing backends is good and missng in my approach currently. I would not put it into the board file though but make it generic for other boards as well and only define the needed informations in the board file. Please see my other mail for details. Comments appreciated!

Comments sent in other mails;) I agree that since there is interest in implementing DFU in u-boot the code which in fact is not board specific can and should be generalized. We need to think of an appropriate place in the tree to store it, though.

Quoted 12 lines

common/Makefile COBJS-y += cmd_usb.o · line 165

Quoted 10 lines
common/Makefile +1 -0
index ae795e0..de926d9 100644
@@ -162,6 +162,7 @@ COBJS-y += cmd_usb.o
 COBJS-y += usb.o
 COBJS-$(CONFIG_USB_STORAGE) += usb_storage.o
 endif
+COBJS-$(CONFIG_CMD_DEVICE_FIRMWARE_UPGRADE) += cmd_dfu.o

CONFIG_CMD_DFU instead? Looks a bit long to me.

ACK. No problem with changing that.

Quoted 7 lines

I already wrote a bit about my approach here. At OpenMoko we used a button to enter u-boot during startup when pressed. This offered a usbtty interface as usb gadget as well as the runtime descripto for DFU. With dfu-util it was possible to iniate the DFU download or upload procedure while being in the mode. Another option would be to directly jump into DFU mode when a button is pressed.

Please see my comment in the other mail. A generic way to handle "a button pressed" seems necessary. Actually in my opinion just providing the "dfu" command is enough. If, in some hardware, initiating the runtime mode is desired by pressing some keys, then the board initialization code should check for such an event and if it happens then just programmatically run the dfu command. What do you think?

Quoted 3 lines

common/Makefile COBJS-y += cmd_usb.o · line 166

Quoted 1 line
common/Makefile +1 -0
+/* #include "usbstring.c" */

Not needed, can be removed.

I agree to coding style/reduntant code comments; will not specifically comment on them unless I disagree.

Quoted 7 lines

common/Makefile COBJS-y += cmd_usb.o

Quoted 4 lines
common/Makefile +4 -0
+static const struct dfu_function_descriptor dfu_func = {
+	.bLength =		sizeof dfu_func,
+	.bDescriptorType =	DFU_DT_FUNC,
+	.bmAttributes =		DFU_BIT_WILL_DETACH |

Here are you setting the WILL_DETACH bit. Is the device really detaching itself? Its not obvious to me from the code that it does.

It seems it does not. The dfu-util session says "Resetting USB", so I assume that resetting is initiated by the host, not by the device. To be removed.

Quoted 8 lines

common/Makefile

Quoted 6 lines
common/Makefile +6 -0
+static char manufacturer[50];
+static const char longname[] = "DFU Gadget";
+/* default serial number takes at least two packets */
+static const char serial[] = "0123456789.0123456789.012345678";
+static const char dfu_name[] = "Device Firmware Upgrade";
+static const char shortname[] = "dfu";

This surely should be come from the board configuration?

Yes and no - depends on whether we want to version the gadget itself or the flashing backend, or both. As probably the last option is best, the answer is probably "yes". We need some interface then.

Quoted 10 lines

common/Makefile

Quoted 5 lines
common/Makefile +5 -0
+static bool is_runtime(struct dfu_dev *dev)
+{
+	return dev->dfu_state == DFU_STATE_appIDLE ||
+		dev->dfu_state == DFU_STATE_appDETACH;
+}

Hmm, here you are checking if you are in run-time mode. In the introduction you wrote that it is in DFU mode when the dfu command is executed. When does it enter the run-time mode? After the first flashing?

I think this is over-interpretation of what I wrote in introduction. I gave an example dfu-util session when the device is already in DFU mode. I didn't say that it only has DFU mode. It has both.

Quoted 9 lines

common/Makefile

Quoted 3 lines
common/Makefile +3 -0
+	/* send status response */
+	dstat->bStatus = dev->dfu_status;
+	/* FIXME: set dstat->bwPollTimeout */

This really needs to be set. It was a nightmare with dfu-util not having it set with the initial u-boot implementation. setting the correct values here is not that easy though. It depends on the flash layer and if can get the information about the maximal time a flash write can take.

Not sure what to do at this moment, either.

Quoted 2 lines

common/Makefile

Quoted 2 lines
common/Makefile +2 -0
+		case USB_REQ_DFU_DETACH:
+			/* Proprietary extension: 'detach' from idle mode

and

Quoted 1 line

common/Makefile

Quoted 1 line
common/Makefile +1 -0
+			 * get back to runtime mode in case of USB Reset.

As

Quoted 3 lines

common/Makefile

Quoted 1 line
common/Makefile +1 -0
+			 * much as I dislike this, we just can't use every

USB

common/Makefile

Quoted 1 line
common/Makefile +1 -0
+			 * bus reset to switch back to runtime mode, since

at

Quoted 10 lines

common/Makefile

Quoted 1 line
common/Makefile +1 -0
+			 * least the Linux USB stack likes to send a number

of

common/Makefile

Quoted 2 lines
common/Makefile +2 -0
+			 * resets in a row :(
+			 */

OK, this makes it clear that you took the DFU state machine from Haralds implementation I'm also using. :)

It would be nice if the related copyright holder would be stated in the header of the file.

You are absolutely right. My gadget implementation is also based on Gadget Zero from Linux and I did mention that, but missed including appropriate information regarding the DFU state machine.

Quoted 9 lines

common/Makefile

Quoted 5 lines
common/Makefile +4 -0
+int __init dfu_init(void)
+{
+	return usb_gadget_register_driver(&dfu_driver);
+}

All this is absed on the re-written gadget layer? It loks as if all kind of kernel stuff is brought over here. Is this gadget layer already merged into u-boot mainline?

I think that the gadget framework works well and can be successfully reused in u-boot. What other interface would you see to talk to UDC? Also, USB mass storage implementation can be easy done with usb gadget.

Actually, the original intention was to implement the dfu both in u-boot and in kernel. I did post my kernel implementation to linux-usb and did give example of being useful. Even though this kind of a driver could seem controversial, there are other drivers based on similar philosophy
which are in the mainline kernel. Anyway, Felipe Balbi, the usb maintainer in
Linux, did not approve because he says this kind of things belongs to userspace. Still, I see the gadget framework suitable
to do the job for us here.

Quoted 6 lines
Quoted 4 lines

+/*
+ * end compatibility layer
+ */

Urgs, is this all needed? Do we really want to have all this around. This is not the kernel. U-Boots debug infrastrcuture should be used.

Please see the comment above. In such circumstances, we probably don't.

Quoted 5 lines
Quoted 1 line

+#define DRIVER_VERSION "Marzanna"

Maybe that helps you internal but it does not make much sense for people outside Samsung. :)

The code I started to work on had an equally nice name here, which was like "Victory Day" or something. "Marzanna" is one of female names in Polish and it was Marzanna's name day when I started work on dfu. Self explanatory ;)

Quoted 5 lines

It was good to see that the DFU state machine was taken from Haralds patches. That should make it a lot easier to find a common ground between the implementations. And I agree that splitting out the DFU protocol from the flashing parts makes sense.

Absolutely yes.

Quoted 3 lines

How would you like to proceed in getting our stuff merged and finally integrated into mainline?

You mentioned that your DFU implementation does things which mine doesn't. Can you please tell something more? As far as I understand what you wrote, there is something in your DFU implementation which does more, and also we use different media as backends.

Thank you,

Andrzej

#12 lore raw

PATCH 1/1 dfu: initial implementation

Hello.

Quoted 15 lines · Andrzej Pietrasiewicz
Quoted 13 lines · Stefan Schmidt

common/Makefile COBJS-y += cmd_usb.o · line 165

Quoted 9 lines · Andrzej Pietrasiewicz
common/Makefile +1 -0
index ae795e0..de926d9 100644
@@ -162,6 +162,7 @@ COBJS-y += cmd_usb.o
 COBJS-y += usb.o
 COBJS-$(CONFIG_USB_STORAGE) += usb_storage.o
 endif
+COBJS-$(CONFIG_CMD_DEVICE_FIRMWARE_UPGRADE) += cmd_dfu.o

CONFIG_CMD_DFU instead? Looks a bit long to me.

Quoted 1 line

ACK. No problem with changing that.

OK

Quoted 7 lines
Quoted 6 lines

I already wrote a bit about my approach here. At OpenMoko we used a button to enter u-boot during startup when pressed. This offered a usbtty interface as usb gadget as well as the runtime descripto for DFU. With dfu-util it was possible to iniate the DFU download or upload procedure while being in the mode. Another option would be to directly jump into DFU mode when a button is pressed.

Quoted 7 lines

Please see my comment in the other mail. A generic way to handle "a button pressed" seems necessary. Actually in my opinion just providing the "dfu" command is enough. If, in some hardware, initiating the runtime mode is desired by pressing some keys, then the board initialization code should check for such an event and if it happens then just programmatically run the dfu command. What do you think?

Lets go with your dfu implementation and the dfu command. I will prepare patches against is if I see something missing from my patch.

Quoted 6 lines
Quoted 3 lines

common/Makefile COBJS-y += cmd_usb.o · line 166

Quoted 1 line
common/Makefile +1 -0
+/* #include "usbstring.c" */

Not needed, can be removed.

I agree to coding style/reduntant code comments; will not specifically comment on them unless I disagree.

Thanks.

Quoted 11 lines
Quoted 7 lines

common/Makefile COBJS-y += cmd_usb.o

Quoted 4 lines
common/Makefile +4 -0
+static const struct dfu_function_descriptor dfu_func = {
+	.bLength =		sizeof dfu_func,
+	.bDescriptorType =	DFU_DT_FUNC,
+	.bmAttributes =		DFU_BIT_WILL_DETACH |

Here are you setting the WILL_DETACH bit. Is the device really detaching itself? Its not obvious to me from the code that it does.

It seems it does not. The dfu-util session says "Resetting USB", so I assume that resetting is initiated by the host, not by the device. To be removed.

So far I also put this aside. Its only a 1.1 feature which can be added with another patch when the basic stuff is working and merged.

Quoted 9 lines
Quoted 8 lines

common/Makefile

Quoted 6 lines
common/Makefile +6 -0
+static char manufacturer[50];
+static const char longname[] = "DFU Gadget";
+/* default serial number takes at least two packets */
+static const char serial[] = "0123456789.0123456789.012345678";
+static const char dfu_name[] = "Device Firmware Upgrade";
+static const char shortname[] = "dfu";

This surely should be come from the board configuration?

Quoted 3 lines

Yes and no - depends on whether we want to version the gadget itself or the flashing backend, or both. As probably the last option is best, the answer is probably "yes". We need some interface then.

As I already stated this part is not worked out in my head yet. Feel free to propose something or we will fix it while working on it.

Quoted 11 lines
Quoted 10 lines

common/Makefile

Quoted 5 lines
common/Makefile +5 -0
+static bool is_runtime(struct dfu_dev *dev)
+{
+	return dev->dfu_state == DFU_STATE_appIDLE ||
+		dev->dfu_state == DFU_STATE_appDETACH;
+}

Hmm, here you are checking if you are in run-time mode. In the introduction you wrote that it is in DFU mode when the dfu command is executed. When does it enter the run-time mode? After the first flashing?

Quoted 3 lines

I think this is over-interpretation of what I wrote in introduction. I gave an example dfu-util session when the device is already in DFU mode. I didn't say that it only has DFU mode. It has both.

Yeah, sorry for that. I indeed misread this part of your implementation.

Quoted 11 lines
Quoted 9 lines

common/Makefile

Quoted 3 lines
common/Makefile +3 -0
+	/* send status response */
+	dstat->bStatus = dev->dfu_status;
+	/* FIXME: set dstat->bwPollTimeout */

This really needs to be set. It was a nightmare with dfu-util not having it set with the initial u-boot implementation. setting the correct values here is not that easy though. It depends on the flash layer and if can get the information about the maximal time a flash write can take.

Not sure what to do at this moment, either.

If we don't have a back channel from the actual flashing subsystem we need to oriantate the values on the hardware spec on own measurements I fear. Soemthing that need to be set in the board file then.

Quoted 17 lines
Quoted 2 lines

common/Makefile

Quoted 2 lines
common/Makefile +2 -0
+		case USB_REQ_DFU_DETACH:
+			/* Proprietary extension: 'detach' from idle mode

and

Quoted 1 line

common/Makefile

Quoted 1 line
common/Makefile +1 -0
+			 * get back to runtime mode in case of USB Reset.

As

Quoted 3 lines

common/Makefile

Quoted 1 line
common/Makefile +1 -0
+			 * much as I dislike this, we just can't use every

USB

common/Makefile

Quoted 1 line
common/Makefile +1 -0
+			 * bus reset to switch back to runtime mode, since

at

Quoted 8 lines

common/Makefile

Quoted 1 line
common/Makefile +1 -0
+			 * least the Linux USB stack likes to send a number

of

common/Makefile

Quoted 2 lines
common/Makefile +2 -0
+			 * resets in a row :(
+			 */

OK, this makes it clear that you took the DFU state machine from Haralds implementation I'm also using. :)

Quoted 6 lines
Quoted 2 lines

It would be nice if the related copyright holder would be stated in the header of the file.

You are absolutely right. My gadget implementation is also based on Gadget Zero from Linux and I did mention that, but missed including appropriate information regarding the DFU state machine.

OK. Would be good to add this for the next round.

Quoted 9 lines
Quoted 8 lines

common/Makefile

Quoted 4 lines
common/Makefile +4 -0
+int __init dfu_init(void)
+{
+	return usb_gadget_register_driver(&dfu_driver);
+}

All this is absed on the re-written gadget layer? It loks as if all kind of kernel stuff is brought over here. Is this gadget layer already merged into u-boot mainline?

Quoted 3 lines

I think that the gadget framework works well and can be successfully reused in u-boot. What other interface would you see to talk to UDC? Also, USB mass storage implementation can be easy done with usb gadget.

I don't wanted to argue against a good gadget framework. I was just wondering if it already is in mainline and if not what is blocking it.

My patch has some additions to usbtty for the runtime descriptor to show up and all other parts are handled over ep0. The gadget framework would make this easier and better abstracted to work toegther with different usb gadget drivers. The only fear I have is to depend on soemthing not yet in u-boot mainline will make it harder to get the dfu implementation merged.

Quoted 10 lines
Quoted 5 lines
Quoted 1 line

+#define DRIVER_VERSION "Marzanna"

Maybe that helps you internal but it does not make much sense for people outside Samsung. :)

The code I started to work on had an equally nice name here, which was like "Victory Day" or something. "Marzanna" is one of female names in Polish and it was Marzanna's name day when I started work on dfu. Self explanatory ;)

Hehe :)

Quoted 7 lines
Quoted 2 lines

How would you like to proceed in getting our stuff merged and finally integrated into mainline?

You mentioned that your DFU implementation does things which mine doesn't. Can you please tell something more? As far as I understand what you wrote, there is something in your DFU implementation which does more, and also we use different media as backends.

One thin was the runtime and dfu mode thing. I was wrong there as yours also do both. For the flashing backend I have the alt settings dynamically generated from available mtdparts and use the new nand write functions skipping over bad blocks. For the core DFU protocol I'm not sure about the differences yet. But as I said, prepare I stand alone patch of yours and I will add send patches to add missing stuff of the DFU implementation, if any.

My patch should be on the list later today or tomorrow to let you see what I have.

regards
Stefan Schmidt

#14 lore raw

PATCH 1/1 dfu: initial implementation

Hello.

Quoted 6 lines · Andrzej Pietrasiewicz
Quoted 3 lines · Andrzej Pietrasiewicz

Sorry about that. I forgot to mention the reference. It is
http://patchwork.ozlabs.org/patch/122080

Sorry again. I meant http://patchwork.ozlabs.org/patch/122079

Hmm, applied this one but the DFU patch stills fails to apply in goni.c and goni.h. Seems there is something else applied that is not in mainline yet.

I can't test the goni part anyway as I don't have the hardware so I will split it out of the patch and test the dfu command and its implementation.

regards
Stefan Schmidt

#15 lore raw

PATCH 1/1 dfu: initial implementation

Hello Stefan,

Quoted 9 lines · Stefan Schmidt
Quoted 5 lines
Quoted 4 lines

Sorry about that. I forgot to mention the reference. It is
http://patchwork.ozlabs.org/patch/122080

Sorry again. I meant http://patchwork.ozlabs.org/patch/11122079

Hmm, applied this one but the DFU patch stills fails to apply in goni.c and goni.h. Seems there is something else applied that is not in mainline yet.

Ok, here is the full story: this http://patchwork.ozlabs.org/patch/11122079
is the second patch in a series, the first being
http://patchwork.ozlabs.org/patch/122080 and you actually need both.

I pulled the latest git.denx.de tree and had to also apply a patch which Defines CONFIG_SYS_CACHELINE_SIZE for Goni target. It can be found
here: http://article.gmane.org/gmane.comp.boot-loaders.u-boot/112755/match=
I know this is confusing. Sorry about that.

Andrzej

#16 lore raw

PATCH 1/1 dfu: initial implementation

In reply to Stefan Schmidt

Dear Stefan Schmidt,

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

While I think a dfu command is usefull I don't like the need to execute it before any DFU interaction can happen. That may be an option during development but for field upgrades or receovery it is not.

Yes, a command is the natural way in U-Boot to start any activities.

Quoted 2 lines

I already wrote a bit about my approach here. At OpenMoko we used a button to enter u-boot during startup when pressed. This offered a

This is just another way to start a command.

Quoted 4 lines

usbtty interface as usb gadget as well as the runtime descripto for DFU. With dfu-util it was possible to iniate the DFU download or upload procedure while being in the mode. Another option would be to directly jump into DFU mode when a button is pressed.

NAK. Let's stick to standard interfaces.

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
Each honest calling, each walk of life, has its own  elite,  its  own
aristocracy based on excellence of performance. - James Bryant Conant
#17 lore raw

PATCH 1/1 dfu: initial implementation

Hello.

Quoted 7 lines · Wolfgang Denk
Quoted 5 lines · In message <[email protected]> you wrote:

While I think a dfu command is usefull I don't like the need to execute it before any DFU interaction can happen. That may be an option during development but for field upgrades or receovery it is not.

Quoted 6 lines

Yes, a command is the natural way in U-Boot to start any activities.

Quoted 2 lines

I already wrote a bit about my approach here. At OpenMoko we used a button to enter u-boot during startup when pressed. This offered a

This is just another way to start a command.

So bounding a key press to the command would be the way to achieve what some people want. Fine with me.

Quoted 6 lines
Quoted 4 lines

usbtty interface as usb gadget as well as the runtime descripto for DFU. With dfu-util it was possible to iniate the DFU download or upload procedure while being in the mode. Another option would be to directly jump into DFU mode when a button is pressed.

NAK. Let's stick to standard interfaces.

What do you mean here? You don't want the DFU descripto available with USBTTY or you don't want to start DFU directly in DFU mode?

regards
Stefan Schmidt

#18 lore raw

PATCH 1/1 dfu: initial implementation

Dear Stefan Schmidt,

Quoted 10 lines · In message <[email protected]> you wrote:
Quoted 7 lines
Quoted 5 lines

usbtty interface as usb gadget as well as the runtime descripto for DFU. With dfu-util it was possible to iniate the DFU download or upload procedure while being in the mode. Another option would be to directly jump into DFU mode when a button is pressed.

NAK. Let's stick to standard interfaces.

What do you mean here? You don't want the DFU descripto available with USBTTY or you don't want to start DFU directly in DFU mode?

I don't want random transfers of control between unrelated sub-systems. If you want to run a command, then run a command.

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
In the beginning, there was nothing, which exploded.
                                - Terry Pratchett, _Lords and Ladies_
#19 lore raw

PATCH 1/1 dfu: initial implementation

In reply to Andrzej Pietrasiewicz

Dear Andrzej Pietrasiewicz,

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

Device Firmware Upgrade (DFU) is an extension to the USB specification. As of the time of this writing it is documented at

http://www.usb.org/developers/devclass_docs/DFU_1.1.pdf

...

Quoted 8 lines

Please refer to dfu-util documentation for details.
The below ASCII-art presents a connection between the host and the device.

      +-----------------+    <--- DFU --->    +-------------------------+
      |  e.g. dfu-util  |    <=== USB ===>    |          / altsetting 0 |
      |          host   +---------------------+   device - altsetting 1 |
      |   file /        |                     |          \     ...      |
      +-----------------+                     +-------------------------+

Sorry for asking some probably really dumb questions...

If I understand correctly, there is no fundamental reason why the DFU protocol would only be possible to implement over a USB link. Or am I missing something here?

Eventually it should be possible to run this protocol over Ethernet or even over a serial line?

If my assumption is correct, then what would it take to split off protocol part and make it independent of the actual driver interface?

Best regards,

Wolfgang Denk

Signature · 4 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
Today is the yesterday you worried about tomorrow.
#20 lore raw

PATCH 1/1 dfu: initial implementation

Dear Wolfgang Denk,

Please see my comments inline.

Quoted 3 lines · Wolfgang Denk
Quoted 2 lines

http://www.usb.org/developers/devclass_docs/DFU_1.1.pdf

...

DFU is part of USB; an extension to be precise, but an extension bound so tightly to the design and philosophy of USB that it is rather inconceivable to separate the two.

Quoted 7 lines

If I understand correctly, there is no fundamental reason why the DFU protocol would only be possible to implement over a USB link. Or am I missing something here?

Eventually it should be possible to run this protocol over Ethernet or even over a serial line?

Of course there is no such a reason, provided we lay USB over Ethernet or serial line first ;)
Seriously speaking, in view of ties between DFU and USB IMHO it is impossible, or, at least, highly impractical.

Quoted 4 lines

If my assumption is correct, then what would it take to split off protocol part and make it independent of the actual driver interface?

I guess that in the situation given it would be of little use.

Regards,

Andrzej

#21 lore raw

PATCH 1/1 dfu: initial implementation

Dear Andrzej Pietrasiewicz,

Quoted 6 lines · In message <000601cc9abe$4f544bd0$edfce370$%[email protected]> you wrote:
Quoted 2 lines
Quoted 2 lines

http://www.usb.org/developers/devclass_docs/DFU_1.1.pdf

DFU is part of USB; an extension to be precise, but an extension bound so tightly to the design and philosophy of USB that it is rather inconceivable to separate the two.

Could you please be so kind and explain which exact issues you see for such a separation?

Quoted 5 lines
Quoted 2 lines

Eventually it should be possible to run this protocol over Ethernet or even over a serial line?

Of course there is no such a reason, provided we lay USB over Ethernet or serial line first ;)

This is of course not intended. I was thinking about a plain standard UDP based link.

Quoted 2 lines

Seriously speaking, in view of ties between DFU and USB IMHO it is impossible, or, at least, highly impractical.

Can you please support this statement with a few facts?

Quoted 4 lines
Quoted 2 lines

If my assumption is correct, then what would it take to split off protocol part and make it independent of the actual driver interface?

I guess that in the situation given it would be of little use.

What do you think would be of little use?

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
This restaurant was advertising breakfast  any  time.  So  I  ordered
french toast in the renaissance.            - Steven Wright, comedian
#22 lore raw

PATCH 1/1 dfu: initial implementation

Hello.

Quoted 10 lines · Wolfgang Denk
Quoted 6 lines · In message <000601cc9abe$4f544bd0$edfce370$%[email protected]> you wrote:
Quoted 2 lines
Quoted 2 lines

http://www.usb.org/developers/devclass_docs/DFU_1.1.pdf

DFU is part of USB; an extension to be precise, but an extension bound so tightly to the design and philosophy of USB that it is rather inconceivable to separate the two.

Could you please be so kind and explain which exact issues you see for such a separation?

As Andrzej pointed out the DFU spec is written by the USB forum and one can see that there target are USB devices. Let me bring in some facts from the spec.

One part that is not covered here is the DFU suffix mandatory for all firmware files. It gets stripped from the flashing tool before sending it to the device code and thus it was not covered here so far. This suffix holds, among others, fields with the PID of VID of the USB device. Only allowing to be flashed if the device has the correct IDs. How would assign the USB IDs to a network based approach for example?

The core part, I understand, is the DFU state machine you are referring to. This sate machine relies on the USB descriptors in run-time or DFU mode. It is hardcoded wot USB descriptors using bDeviceClass, bDeviceSubClass, idVendor and idProduct to name a few. (See p. 14, 15 of the 1.1 spec).

In the reconfiguration phase the spec mandates the host to issue a DFU_DETACH request on the USB EP0 and then issuing an USB reset. (p.
17)

All bound to the USB spec and hardware for using the spec in a standard compliant way.

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

Eventually it should be possible to run this protocol over Ethernet or even over a serial line?

Of course there is no such a reason, provided we lay USB over Ethernet or serial line first ;)

This is of course not intended. I was thinking about a plain standard UDP based link.

As always in computers one could try to come up with solutions to replace the USB hardcoded parts with something else for a UDP based solution. Its just now longer compliant with the DFU spec. All DFU flashing utils out there are only working over USB. All the windows flash utils as well as dfu-programmer, a small dfu util from the bluez project and also dfu-util. The last ones are using libusb for this.

The main point is that if you replace the USB related parts with something else not much of DFU stays anymore. It would be a different flashing procedure. And for systems with a network interface available there are way better ways to flash it then DFU. Even TFP would be more convenient for them. You are way more flexible with Ethernet here.

DFU was designed for really small devices with minimal software originally. Best examples are Bluetooth and WiFi USB dongles which can be flashed this way if they. Its just that it becomes more popular as many consumer electronics device are coming with a USB port but no Ethernet these days.

regards
Stefan Schmidt

#23 lore raw

PATCH 1/1 dfu: initial implementation

Dear Stefan Schmidt,

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

Could you please be so kind and explain which exact issues you see for such a separation?

As Andrzej pointed out the DFU spec is written by the USB forum and one can see that there target are USB devices. Let me bring in some facts from the spec.

...

Quoted 2 lines

All bound to the USB spec and hardware for using the spec in a standard compliant way.

Yes, indeed.

But I feel you stick way too close to this spec, and are not trying to abstract awway the low level details a bit.

Yes, this spec was written by the USB forum, and they did not even attempt to look over the rim of their plates. But does that mean we all have to do the same? I'm not willing to accept such a narrow view.

Let's - just as a gedankenexperiment - assume our task was to implement a solution that provides similar capabilities as DFU, but must work over serial line, Ethernet, Bluetooth, Infrared, whatever. Assume we thing DFU could be used as base, but we need to find a way to move the USB related things into the USB layer, while things like the state machine, the requests, device status, state transitions, etc. are generic enough.

Yes, the DFU File Suffix contains fields that map nicely to USB devices - but what prevents us to provide similar data on non-USB media, too? We just need to make sure that th code which handles this allows to plug in handlers for other transfer media.

Quoted 3 lines

As always in computers one could try to come up with solutions to replace the USB hardcoded parts with something else for a UDP based solution. Its just now longer compliant with the DFU spec. All DFU

I'm not sure what exactly you want to tell me here.

My idea is that of an implementation that is split into a generic and a device specific part. If implemented corectly, the combination of the generic part with the USB specific part would be strictly conformant to the DFU spec.

Of course, the combination of for example the DFU generic part with an Ethernet network interface would not be conformant to the DFU spec, but it would be something that works in a very similar way. And that would most probably be better than inventing yet another download method.

Quoted 3 lines

flashing utils out there are only working over USB. All the windows flash utils as well as dfu-programmer, a small dfu util from the bluez project and also dfu-util. The last ones are using libusb for this.

That's the status quo. Agreed. Bit this can be changed, extended. Or not?

Quoted 2 lines

The main point is that if you replace the USB related parts with something else not much of DFU stays anymore. It would be a different

I disagree here. DFU is different in a number of aspects, and you know this very well.

Quoted 3 lines

flashing procedure. And for systems with a network interface available there are way better ways to flash it then DFU. Even TFP would be more convenient for them. You are way more flexible with Ethernet here.

I'm not sure what your exact argument is here. I'm not the one asking for DFU support. I would be happy to use TFTP over Ethenret over USB. We're actually doing this in some products.

Quoted 5 lines

DFU was designed for really small devices with minimal software originally. Best examples are Bluetooth and WiFi USB dongles which can be flashed this way if they. Its just that it becomes more popular as many consumer electronics device are coming with a USB port but no Ethernet these days.

Agreed. And if it's becoming more popular, it might even be a good idea to think about a less restricted implementation, which opens new oportunites.

Yes, I think we should strive for such a separation of USB transport layer and protocol implementation / state machine / command interface.

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
While money can't buy happiness, it certainly lets  you  choose  your
own form of misery.
#24 lore raw

PATCH 1/1 dfu: initial implementation

In reply to Wolfgang Denk

Dear Wolfgang Denk,

Please see my comments inline.

Quoted 8 lines
Quoted 2 lines

DFU is part of USB; an extension to be precise, but an extension

bound

Quoted 2 lines

so tightly to the design and philosophy of USB that it is rather inconceivable to separate the two.

Could you please be so kind and explain which exact issues you see for such a separation?

First of all, I think that it is more proper to speak of communications part and state machine part. They are bound together by the standard. For example, in the dfuIDLE state, the receipt of DFU_DNLOAD request with wLength == 0 triggers a "device stalls the control pipe" action. This kind of stuff is very USB specific - I am not sure what would "the control pipe" be for serial/Ethernet, nor what "stall the control pipe" would mean for either of them.
Another example, in the dfuMANIFEST state, when the status poll timeout occurs and bitManifestationTolerant == 1 then "device can still communicate via the USB".

The communications part is of course the very USB itself, so the only thing which could be extracted is the state machine part. But, then, in order not to be bound to USB but reusable, it should be abstract. Being abstract, it would not be DFU anymore, but some "Generic Update Protocol" that you have on your mind,
but we don't know of. Is there any standard for such a protocol?

Quoted 5 lines
Quoted 3 lines

Seriously speaking, in view of ties between DFU and USB IMHO it is impossible, or, at least, highly impractical.

Can you please support this statement with a few facts?

Please see my comment above.

Quoted 8 lines
Quoted 2 lines
Quoted 2 lines

If my assumption is correct, then what would it take to split off protocol part and make it independent of the actual driver

interface?

Quoted 2 lines

I guess that in the situation given it would be of little use.

What do you think would be of little use?

As far as I understand you correctly, you would like the state machine code extracted from the initial DFU implementation posted on this mailing list. And then you would like the DFU implemented in terms of this abstract state machine. This means actually more code, probably not a desired thing. The generic state machine would also require some accompanying generic data structures, and this would also mean code to translate back and forth between USB/DFU (DFU _is_ USB) and state machine's data structures. All in all, this means adding more complexity and code. This looks like a premature optimization (which is widely known to be the root of all evil), while at the moment, there are no use cases to justify it. Should the use cases appear, the code can be reworked based on the needs of both USB/DFU and new state machine users.

Regards,

Andrzej

#25 lore raw

PATCH 1/1 dfu: initial implementation

Hi Andrzej,

[...]

Quoted 9 lines
Quoted 8 lines
Quoted 2 lines
Quoted 2 lines

If my assumption is correct, then what would it take to split off protocol part and make it independent of the actual driver

interface?

Quoted 2 lines

I guess that in the situation given it would be of little use.

What do you think would be of little use?

Quoted 13 lines

As far as I understand you correctly, you would like the state machine code extracted from the initial DFU implementation posted on this mailing list. And then you would like the DFU implemented in terms of this abstract state machine. This means actually more code, probably not a desired thing. The generic state machine would also require some accompanying generic data structures, and this would also mean code to translate back and forth between USB/DFU (DFU _is_ USB) and state machine's data structures. All in all, this means adding more complexity and code. This looks like a premature optimization (which is widely known to be the root of all evil), while at the moment, there are no use cases to justify it. Should the use cases appear, the code can be reworked based on the needs of both USB/DFU and new state machine users.

Just to add my personal opinion here - I am occassionally lobbying for a long time on this mailing list that mainline U-Boot gains DFU support, so please let's get this working with the existing tools and in the existing contexts, i.e. USB. Personally I consider it to be completely out of scope to dis-entangle DFU from its specification and abstract it into something which it _could_ be, but really _isn't_.

When the implementation for USB is here, we all have much better grounds to discuss possible generalizations (although I doubt that there are many).

Cheers
Detlev

Signature · 6 lines
Perfecting oneself is as much unlearning as it is learning. 
                                        -- Edsger Dijkstra 
--
DENX Software Engineering GmbH,      MD: Wolfgang Denk & Detlev Zundel
HRB 165235 Munich,  Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-40 Fax: (+49)-8142-66989-80 Email: dzu at denx.de