It might be preferable to actually make "fel_version" global.
aw_fel_get_sram_info() could operate on that data, and cache its result,
so the soc_sram_info pointer gets initialized only once (upon the first
request that makes actual use of sram_info). This way it's safe to call
the function multiple times / wherever needed.

What do you think?

Regards, B. Nortmann


Am 14.09.2015 um 11:55 schrieb Bernhard Nortmann:
Hello Siarhei!

Am 13.09.2015 um 23:42 schrieb Siarhei Siamashka:
With this change, we are going to print "Warning: no 'soc_sram_info'
data for your SoC" message regardless of what kind of FEL command
is requested in the command line. Simple FEL commands don't need any
SoC-specific information, so we are going to be nagging the users
of the new SoC variants.

On the other hand, maybe this is not too bad?

Good catch. I also think we could "get away" with that, but it may
not be the nicest way to deal with it. I would think that handling
this might be achieved by initializing sram_info to NULL, and only
call aw_fel_get_sram_info() later when sram_info is actually needed
("spl"/"uboot", or script transfer with "write")? It leads to slightly
more complicated code, but seems reasonable.

Regards, B. Nortmann

--
You received this message because you are subscribed to the Google Groups 
"linux-sunxi" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.
>From f53935b6d95930a2c0e4597165f5324451e1a70e Mon Sep 17 00:00:00 2001
From: Bernhard Nortmann <[email protected]>
Date: Wed, 9 Sep 2015 13:37:36 +0200
Subject: [RFC PATCH v3 2/3] sunxi-tools: factor out persistent version and
 sram_info information

FEL version information and SoC-specific memory layout are used
across several places in the fel utility code. To avoid having
to retrieve this information repeatedly, this patch introduces
a global variable that holds a struct aw_fel_version, and makes
aw_fel_get_sram_info() cache its result.

Signed-off-by: Bernhard Nortmann <[email protected]>
---
 fel.c | 50 ++++++++++++++++++++++++++++----------------------
 1 file changed, 28 insertions(+), 22 deletions(-)

diff --git a/fel.c b/fel.c
index 52b785f..48c0add 100644
--- a/fel.c
+++ b/fel.c
@@ -60,6 +60,7 @@ struct aw_fel_version {
 static const int AW_USB_READ = 0x11;
 static const int AW_USB_WRITE = 0x12;
 
+static struct aw_fel_version fel_version; /* global FEL/SoC version info */
 static int AW_USB_FEL_BULK_EP_OUT;
 static int AW_USB_FEL_BULK_EP_IN;
 static int timeout = 60000;
@@ -222,13 +223,10 @@ void aw_fel_get_version(libusb_device_handle *usb, struct 
aw_fel_version *buf)
        buf->pad[1] = le32toh(buf->pad[1]);
 }
 
-void aw_fel_print_version(libusb_device_handle *usb)
+void aw_fel_print_version(struct aw_fel_version *buf)
 {
-       struct aw_fel_version buf;
-       aw_fel_get_version(usb, &buf);
-
        const char *soc_name="unknown";
-       switch (buf.soc_id) {
+       switch (buf->soc_id) {
        case 0x1623: soc_name="A10";break;
        case 0x1625: soc_name="A13";break;
        case 0x1633: soc_name="A31";break;
@@ -241,9 +239,9 @@ void aw_fel_print_version(libusb_device_handle *usb)
        }
 
        printf("%.8s soc=%08x(%s) %08x ver=%04x %02x %02x scratchpad=%08x %08x 
%08x\n",
-               buf.signature, buf.soc_id, soc_name, buf.unknown_0a,
-               buf.protocol, buf.unknown_12, buf.unknown_13,
-               buf.scratchpad, buf.pad[0], buf.pad[1]);
+               buf->signature, buf->soc_id, soc_name, buf->unknown_0a,
+               buf->protocol, buf->unknown_12, buf->unknown_13,
+               buf->scratchpad, buf->pad[0], buf->pad[1]);
 }
 
 void aw_fel_read(libusb_device_handle *usb, uint32_t offset, void *buf, size_t 
len)
@@ -501,20 +499,25 @@ soc_sram_info generic_sram_info = {
        .swap_buffers = generic_sram_swap_buffers,
 };
 
-soc_sram_info *aw_fel_get_sram_info(libusb_device_handle *usb)
+soc_sram_info *aw_fel_get_sram_info(void)
 {
-       int i;
-       struct aw_fel_version buf;
-
-       aw_fel_get_version(usb, &buf);
-
-       for (i = 0; soc_sram_info_table[i].swap_buffers; i++)
-               if (soc_sram_info_table[i].soc_id == buf.soc_id)
-                       return &soc_sram_info_table[i];
+       /* persistent sram_info, retrieves result pointer once and caches it */
+       static soc_sram_info *result = NULL;
+       if (result == NULL) {
+               int i;
+               for (i = 0; soc_sram_info_table[i].swap_buffers; i++)
+                       if (soc_sram_info_table[i].soc_id == 
fel_version.soc_id) {
+                               result = &soc_sram_info_table[i];
+                               break;
+                       }
 
-       printf("Warning: no 'soc_sram_info' data for your SoC (id=%04X)\n",
-              buf.soc_id);
-       return &generic_sram_info;
+               if (!result) {
+                       printf("Warning: no 'soc_sram_info' data for your SoC 
(id=%04X)\n",
+                              fel_version.soc_id);
+                       result = &generic_sram_info;
+               }
+       }
+       return result;
 }
 
 static uint32_t fel_to_spl_thunk[] = {
@@ -730,7 +733,7 @@ void aw_restore_and_enable_mmu(libusb_device_handle *usb,
 void aw_fel_write_and_execute_spl(libusb_device_handle *usb,
                                  uint8_t *buf, size_t len)
 {
-       soc_sram_info *sram_info = aw_fel_get_sram_info(usb);
+       soc_sram_info *sram_info = aw_fel_get_sram_info();
        sram_swap_buffers *swap_buffers;
        char header_signature[9] = { 0 };
        size_t i, thunk_size;
@@ -1050,6 +1053,9 @@ int main(int argc, char **argv)
                exit(1);
        }
 
+       /* retrieve persistent version information */
+       aw_fel_get_version(handle, &fel_version);
+
        if (argc > 1 && (strcmp(argv[1], "--verbose") == 0 ||
                         strcmp(argv[1], "-v") == 0)) {
                verbose = 1;
@@ -1070,7 +1076,7 @@ int main(int argc, char **argv)
                        aw_fel_execute(handle, strtoul(argv[2], NULL, 0));
                        skip=3;
                } else if (strncmp(argv[1], "ver", 3) == 0 && argc > 1) {
-                       aw_fel_print_version(handle);
+                       aw_fel_print_version(&fel_version);
                        skip=1;
                } else if (strcmp(argv[1], "write") == 0 && argc > 3) {
                        double t1, t2;
-- 
2.4.6

Reply via email to