usb: Add support to set proper serial-number 21/243121/3 accepted/tizen/unified/20200904.154058 submit/tizen/20200904.033042
authorDongwoo Lee <dwoo08.lee@samsung.com>
Thu, 3 Sep 2020 07:43:59 +0000 (16:43 +0900)
committerDongwoo Lee <dwoo08.lee@samsung.com>
Fri, 4 Sep 2020 02:49:47 +0000 (11:49 +0900)
Now, we can provide serial-number to TFM's usb interface by using
option 's'.

Change-Id: Ie88350fded96999eb24b1a3f834598479c80177c
Signed-off-by: Dongwoo Lee <dwoo08.lee@samsung.com>
scripts/flash-init.sh
src/main.c
src/usb.c

index 3d0084d..c316076 100755 (executable)
@@ -169,7 +169,11 @@ print_info() {
 #      do_flash
 #------------------------------------------------
 do_flash() {
-       "$FLASH_MANAGER"
+       if [ -n "$SERIAL" ]; then
+               FM_OPTS="-s ${SERIAL}"
+       fi
+
+       bash -c "${FLASH_MANAGER} ${FM_OPTS}"
 
        #Control-flow will never reach here, because fm only terminates by rebooting
 
index c9a3865..e7d276d 100644 (file)
@@ -31,6 +31,7 @@ int _main(int argc, char *argv[])
        struct tfm_interface *intf;
        const char *part_table = "/usr/share/partition.info";
        char *opt_table = NULL;
+       char *opt_serial = NULL;
        int ret, opt;
 
        supported_interfaces = tfm_interface_init();
@@ -39,7 +40,7 @@ int _main(int argc, char *argv[])
                goto out;
        }
 
-       while ((opt = getopt(argc, argv, "p:i:")) != -1) {
+       while ((opt = getopt(argc, argv, "p:i:s:")) != -1) {
                switch (opt) {
                case 'i':
                        if (optarg) {
@@ -51,13 +52,13 @@ int _main(int argc, char *argv[])
                                } else {
                                        fprintf(stderr, "Out of memory\n");
                                        ret = -1;
-                                       goto out_intfexit;
+                                       goto out_optfree;
                                }
                        } else {
                                fprintf(stderr,
                                        "path should be specified with '-i'\n");
                                ret = -1;
-                               goto out_intfexit;
+                               goto out_optfree;
                        }
                        break;
                case 'p':
@@ -83,6 +84,32 @@ int _main(int argc, char *argv[])
                                                        "net", (void *)val);
                        break;
                }
+               case 's':
+                       if (!tfm_interface_is_available(supported_interfaces,
+                                                      "usb")) {
+                               fprintf(stderr,
+                                       "'-s' option is ignored since usb interface is not available.\n");
+                               continue;
+                       }
+
+                       if (optarg) {
+                               if (opt_serial)
+                                       free(opt_serial);
+                               opt_serial = strdup(optarg);
+                               if (!opt_serial) {
+                                       fprintf(stderr, "Out of memory\n");
+                                       ret = -1;
+                                       goto out_optfree;
+                               }
+                       } else {
+                               fprintf(stderr,
+                                       "serial should be specified with '-s'\n");
+                               ret = -1;
+                               goto out_optfree;
+                       }
+                       tfm_interface_set_private(supported_interfaces,
+                                                 "usb", opt_serial);
+                       break;
                default:
                        ret = -1;
                        goto out_optfree;
@@ -122,7 +149,9 @@ out_dfuexit:
 out_optfree:
        if (opt_table)
                free(opt_table);
-out_intfexit:
+       if (opt_serial)
+               free(opt_serial);
+
        tfm_interface_exit(supported_interfaces);
 
 out:
index 0c36fa0..791df5b 100644 (file)
--- a/src/usb.c
+++ b/src/usb.c
@@ -39,6 +39,9 @@
 #define TFMFFS_BIND_LINK       "tfm.ffs"
 #define TFMFFS_VID 0x04e8
 #define TFMFFS_PID 0x685d
+#define TFMFFS_DEFAULT_SERIAL  "Unavailable"
+#define TFMFFS_PRODUCT         "USB download gadget"
+#define TFMFFS_MANUFACTURER    "Tizen"
 
 enum {
        TFMFFS_EP0,
@@ -66,9 +69,9 @@ struct usbg_gadget_attrs g_attrs = {
 };
 
 struct usbg_gadget_strs g_strs = {
-       .serial = "Unavailable", /* Serial number */
-       .manufacturer = "Tizen", /* Manufacturer */
-       .product = "iot-refboard" /* Product string */
+       .serial = TFMFFS_DEFAULT_SERIAL,
+       .manufacturer = TFMFFS_MANUFACTURER,
+       .product = TFMFFS_PRODUCT
 };
 
 struct usbg_config_strs c_strs = {
@@ -413,7 +416,22 @@ static int usb_connect(struct tfm_interface *intf)
                goto err_freectx;
        }
 
+       /* If serial is provided as argument, set it to g_strs */
+       if (intf->priv)
+               g_strs.serial = intf->priv;
+
        ret = usbg_create_gadget(s, TFMFFS_GADGET, &g_attrs, &g_strs, &g);
+       /*
+        * In order to prevent that g_strs.serial has dangling pointer
+        * after the provided string is freed, we set g_strs.serial as
+        * default here.
+        * Since g_strs is accessed by only usbg_create_gadget() once,
+        * we can change it safely here.
+        */
+       if (intf->priv) {
+               g_strs.serial = TFMFFS_DEFAULT_SERIAL;
+               intf->priv = NULL;
+       }
        if (ret < 0) {
                fprintf(stderr, "Failed to create USB gadget\n");
                goto err_cleanup;