<p>Hoernchen has uploaded this change for <strong>review</strong>.</p><p><a href="https://gerrit.osmocom.org/c/osmo-asf4-dfu/+/17024">View Change</a></p><pre style="font-family: monospace,monospace; white-space: pre-wrap;">usb serial number descriptor<br><br>Change-Id: I4a23682cee0849c788e5f6c611df4ed1f1cac3da<br>---<br>M config/usbd_config.h<br>M usb_dfu_main.c<br>M usb_start.c<br>3 files changed, 116 insertions(+), 3 deletions(-)<br><br></pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;">git pull ssh://gerrit.osmocom.org:29418/osmo-asf4-dfu refs/changes/24/17024/1</pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;"><span>diff --git a/config/usbd_config.h b/config/usbd_config.h</span><br><span>index f82934c..7ae1b05 100644</span><br><span>--- a/config/usbd_config.h</span><br><span>+++ b/config/usbd_config.h</span><br><span>@@ -163,7 +163,7 @@</span><br><span> // <e> Enable string descriptor of iSerialNum</span><br><span> // <id> usb_dfud_iserialnum_en</span><br><span> #ifndef CONF_USB_DFUD_ISERIALNUM_EN</span><br><span style="color: hsl(0, 100%, 40%);">-#define CONF_USB_DFUD_ISERIALNUM_EN 0</span><br><span style="color: hsl(120, 100%, 40%);">+#define CONF_USB_DFUD_ISERIALNUM_EN 1</span><br><span> #endif</span><br><span> </span><br><span> #ifndef CONF_USB_DFUD_ISERIALNUM</span><br><span>@@ -175,11 +175,11 @@</span><br><span> // <s> Unicode string of iSerialNum</span><br><span> // <id> usb_dfud_iserialnum_str</span><br><span> #ifndef CONF_USB_DFUD_ISERIALNUM_STR</span><br><span style="color: hsl(0, 100%, 40%);">-#define CONF_USB_DFUD_ISERIALNUM_STR "123456789ABCDEF"</span><br><span style="color: hsl(120, 100%, 40%);">+#define CONF_USB_DFUD_ISERIALNUM_STR "dummy"</span><br><span> #endif</span><br><span> </span><br><span> #ifndef CONF_USB_DFUD_ISERIALNUM_STR_DESC</span><br><span style="color: hsl(0, 100%, 40%);">-#define CONF_USB_DFUD_ISERIALNUM_STR_DESC </span><br><span style="color: hsl(120, 100%, 40%);">+#define CONF_USB_DFUD_ISERIALNUM_STR_DESC 12, 0x03, 'd', 0x00, 'u', 0x00, 'm', 0x00, 'm', 0x00, 'y', 0x00,</span><br><span> #endif</span><br><span> </span><br><span> // </e></span><br><span>diff --git a/usb_dfu_main.c b/usb_dfu_main.c</span><br><span>index 19fe437..857230d 100644</span><br><span>--- a/usb_dfu_main.c</span><br><span>+++ b/usb_dfu_main.c</span><br><span>@@ -19,6 +19,7 @@</span><br><span>  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA</span><br><span>  */</span><br><span> </span><br><span style="color: hsl(120, 100%, 40%);">+#include <errno.h></span><br><span> #include "atmel_start.h"</span><br><span> #include "atmel_start_pins.h"</span><br><span> </span><br><span>@@ -85,10 +86,53 @@</span><br><span>     SCB->VTOR = ((uint32_t) application_start_address & SCB_VTOR_TBLOFF_Msk); // re-base the vector table base address</span><br><span>    asm("bx %0"::"r"(*(application_start_address + 1))); // jump to application Reset Handler in the application */</span><br><span> }</span><br><span style="color: hsl(120, 100%, 40%);">+#if defined(SYSMOOCTSIM)</span><br><span style="color: hsl(120, 100%, 40%);">+/* Section 9.6 of SAMD5x/E5x Family Data Sheet */</span><br><span style="color: hsl(120, 100%, 40%);">+static int get_chip_unique_serial(uint8_t *out, size_t len)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+ uint32_t *out32 = (uint32_t *)out;</span><br><span style="color: hsl(120, 100%, 40%);">+    if (len < 16)</span><br><span style="color: hsl(120, 100%, 40%);">+              return -EINVAL;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+     out32[0] = *(uint32_t *)0x008061fc;</span><br><span style="color: hsl(120, 100%, 40%);">+   out32[1] = *(uint32_t *)0x00806010;</span><br><span style="color: hsl(120, 100%, 40%);">+   out32[2] = *(uint32_t *)0x00806014;</span><br><span style="color: hsl(120, 100%, 40%);">+   out32[3] = *(uint32_t *)0x00806018;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ return 0;</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/* same as get_chip_unique_serial but in hex-string format */</span><br><span style="color: hsl(120, 100%, 40%);">+static int get_chip_unique_serial_str(char *out, size_t len)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+        uint8_t buf[16];</span><br><span style="color: hsl(120, 100%, 40%);">+      int rc;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+     if (len < 16*2 + 1)</span><br><span style="color: hsl(120, 100%, 40%);">+                return -EINVAL;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+     rc = get_chip_unique_serial(buf, sizeof(buf));</span><br><span style="color: hsl(120, 100%, 40%);">+        if (rc < 0)</span><br><span style="color: hsl(120, 100%, 40%);">+                return rc;</span><br><span style="color: hsl(120, 100%, 40%);">+    for(int i = 0; i < sizeof(buf); i++)</span><br><span style="color: hsl(120, 100%, 40%);">+               sprintf(&out[i*2], "%02x", buf[i]);</span><br><span style="color: hsl(120, 100%, 40%);">+     return 0;</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+char sernr_buf[16*2+1];</span><br><span style="color: hsl(120, 100%, 40%);">+//unicode for descriptor</span><br><span style="color: hsl(120, 100%, 40%);">+uint8_t sernr_buf_descr[1+1+16*2*2];</span><br><span style="color: hsl(120, 100%, 40%);">+#endif</span><br><span> </span><br><span> int main(void)</span><br><span> {</span><br><span>     atmel_start_init(); // initialise system</span><br><span style="color: hsl(120, 100%, 40%);">+#if defined(SYSMOOCTSIM)</span><br><span style="color: hsl(120, 100%, 40%);">+    get_chip_unique_serial_str(sernr_buf, sizeof(sernr_buf));</span><br><span style="color: hsl(120, 100%, 40%);">+     sernr_buf_descr[0] = sizeof(sernr_buf_descr);</span><br><span style="color: hsl(120, 100%, 40%);">+ sernr_buf_descr[1] = 0x3;</span><br><span style="color: hsl(120, 100%, 40%);">+     for(int i= 2; i < sizeof(sernr_buf_descr); i+=2)</span><br><span style="color: hsl(120, 100%, 40%);">+           sernr_buf_descr[i] = sernr_buf[i >> 1];</span><br><span style="color: hsl(120, 100%, 40%);">+#endif</span><br><span>        if (!check_bootloader()) { // check bootloader</span><br><span>               // blink the LED to tell the user we don't know where the application starts</span><br><span>             while (true) {</span><br><span>diff --git a/usb_start.c b/usb_start.c</span><br><span>index 93059ad..24cde4f 100644</span><br><span>--- a/usb_start.c</span><br><span>+++ b/usb_start.c</span><br><span>@@ -17,6 +17,7 @@</span><br><span>  */</span><br><span> #include "atmel_start.h"</span><br><span> #include "usb_start.h"</span><br><span style="color: hsl(120, 100%, 40%);">+#include "config/usbd_config.h"</span><br><span> </span><br><span> #if CONF_USBD_HS_SP</span><br><span> static uint8_t single_desc_bytes[] = {</span><br><span>@@ -45,6 +46,71 @@</span><br><span> /** Ctrl endpoint buffer */</span><br><span> static uint8_t ctrl_buffer[64];</span><br><span> </span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/* transmit given string descriptor */</span><br><span style="color: hsl(120, 100%, 40%);">+static bool send_str_desc(uint8_t ep, const struct usb_req *req, enum usb_ctrl_stage stage,</span><br><span style="color: hsl(120, 100%, 40%);">+                        const uint8_t *desc)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+     uint16_t len_req = LE16(req->wLength);</span><br><span style="color: hsl(120, 100%, 40%);">+     uint16_t len_desc = desc[0];</span><br><span style="color: hsl(120, 100%, 40%);">+  uint16_t len_tx;</span><br><span style="color: hsl(120, 100%, 40%);">+      bool need_zlp = !(len_req & (CONF_USB_DFUD_BMAXPKSZ0 - 1));</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+     if (len_req <= len_desc) {</span><br><span style="color: hsl(120, 100%, 40%);">+         need_zlp = false;</span><br><span style="color: hsl(120, 100%, 40%);">+             len_tx = len_req;</span><br><span style="color: hsl(120, 100%, 40%);">+     } else {</span><br><span style="color: hsl(120, 100%, 40%);">+              len_tx = len_desc;</span><br><span style="color: hsl(120, 100%, 40%);">+    }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+   if (ERR_NONE != usbdc_xfer(ep, (uint8_t *)desc, len_tx, need_zlp)) {</span><br><span style="color: hsl(120, 100%, 40%);">+          return true;</span><br><span style="color: hsl(120, 100%, 40%);">+  }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+   return false;</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+extern uint8_t sernr_buf_descr[];</span><br><span style="color: hsl(120, 100%, 40%);">+/* call-back for every control EP request */</span><br><span style="color: hsl(120, 100%, 40%);">+static int32_t string_req_cb(uint8_t ep, struct usb_req *req, enum usb_ctrl_stage stage)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+      uint8_t index, type;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+        if (stage != USB_SETUP_STAGE)</span><br><span style="color: hsl(120, 100%, 40%);">+         return ERR_NOT_FOUND;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+       if ((req->bmRequestType & (USB_REQT_TYPE_MASK | USB_REQT_DIR_IN)) !=</span><br><span style="color: hsl(120, 100%, 40%);">+       (USB_REQT_TYPE_STANDARD | USB_REQT_DIR_IN))</span><br><span style="color: hsl(120, 100%, 40%);">+               return ERR_NOT_FOUND;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+       /* abort if it's not a GET DESCRIPTOR request */</span><br><span style="color: hsl(120, 100%, 40%);">+  if (req->bRequest != USB_REQ_GET_DESC)</span><br><span style="color: hsl(120, 100%, 40%);">+             return ERR_NOT_FOUND;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+       /* abort if it's not about a string descriptor */</span><br><span style="color: hsl(120, 100%, 40%);">+ type = req->wValue >> 8;</span><br><span style="color: hsl(120, 100%, 40%);">+     if (type != USB_DT_STRING)</span><br><span style="color: hsl(120, 100%, 40%);">+            return ERR_NOT_FOUND;</span><br><span style="color: hsl(120, 100%, 40%);">+#if 0</span><br><span style="color: hsl(120, 100%, 40%);">+  printf("ep=%02x, bmReqT=%04x, bReq=%02x, wValue=%04x, stage=%d\r\n",</span><br><span style="color: hsl(120, 100%, 40%);">+                ep, req->bmRequestType, req->bRequest, req->wValue, stage);</span><br><span style="color: hsl(120, 100%, 40%);">+#endif</span><br><span style="color: hsl(120, 100%, 40%);">+  /* abort if it's not a standard GET request */</span><br><span style="color: hsl(120, 100%, 40%);">+    index = req->wValue & 0x00FF;</span><br><span style="color: hsl(120, 100%, 40%);">+  switch (index) {</span><br><span style="color: hsl(120, 100%, 40%);">+      case CONF_USB_DFUD_ISERIALNUM:</span><br><span style="color: hsl(120, 100%, 40%);">+                return send_str_desc(ep, req, stage, sernr_buf_descr);</span><br><span style="color: hsl(120, 100%, 40%);">+        default:</span><br><span style="color: hsl(120, 100%, 40%);">+              return ERR_NOT_FOUND;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+static struct usbdc_handler string_req_h = {NULL, (FUNC_PTR)string_req_cb};</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span> /**</span><br><span>  * \brief USB DFU Init</span><br><span>  */</span><br><span>@@ -52,6 +118,9 @@</span><br><span> {</span><br><span>        usbdc_init(ctrl_buffer);</span><br><span>     dfudf_init();</span><br><span style="color: hsl(120, 100%, 40%);">+#if defined(SYSMOOCTSIM)</span><br><span style="color: hsl(120, 100%, 40%);">+       usbdc_register_handler(USBDC_HDL_REQ, &string_req_h);</span><br><span style="color: hsl(120, 100%, 40%);">+#endif</span><br><span> </span><br><span>        usbdc_start(single_desc);</span><br><span>    usbdc_attach();</span><br><span></span><br></pre><p>To view, visit <a href="https://gerrit.osmocom.org/c/osmo-asf4-dfu/+/17024">change 17024</a>. To unsubscribe, or for help writing mail filters, visit <a href="https://gerrit.osmocom.org/settings">settings</a>.</p><div itemscope itemtype="http://schema.org/EmailMessage"><div itemscope itemprop="action" itemtype="http://schema.org/ViewAction"><link itemprop="url" href="https://gerrit.osmocom.org/c/osmo-asf4-dfu/+/17024"/><meta itemprop="name" content="View Change"/></div></div>

<div style="display:none"> Gerrit-Project: osmo-asf4-dfu </div>
<div style="display:none"> Gerrit-Branch: master </div>
<div style="display:none"> Gerrit-Change-Id: I4a23682cee0849c788e5f6c611df4ed1f1cac3da </div>
<div style="display:none"> Gerrit-Change-Number: 17024 </div>
<div style="display:none"> Gerrit-PatchSet: 1 </div>
<div style="display:none"> Gerrit-Owner: Hoernchen <ewild@sysmocom.de> </div>
<div style="display:none"> Gerrit-MessageType: newchange </div>