<div class="gmail_quote">The patch is mostly ack-able, save for a few nits.</div>
<div class="gmail_quote"><br></div><div class="gmail_quote">On Mon, Aug 1, 2011 at 6:05 PM, Stefan Tauner <span dir="ltr"><<a href="mailto:stefan.tauner@student.tuwien.ac.at" target="_blank">stefan.tauner@student.tuwien.ac.at</a>></span> wrote:</div>



<div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">My implementation does not defer the processing until doit(), but after the<br>
argument parsing loop only (doit() should not contain argument checks).<br></blockquote><div><br></div><div>Just FYI -- The reason we deferred processing until doit() was because our usage case calls for reading the ROM content to find pre-existing mappings (e.g. fmap) which exist in the ROM image. It might be useful to eventually do the same thing for cbfs and Intel's flash descriptor.</div>



<div><br></div><div>On Mon, Aug 1, 2011 at 6:05 PM, Stefan Tauner <span dir="ltr"><<a href="mailto:stefan.tauner@student.tuwien.ac.at" target="_blank">stefan.tauner@student.tuwien.ac.at</a>></span> wrote:</div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">



This allows to specify -i and -l parameters in any order.<br>
<br>
I don't like the output in error cases much, any ideas?<br>
example of a good run:<br>
flashrom -p dummy:emulate=SST25VF032B -i normal -w ../testimages/4096kB.rand.img  -l test.layout -i gfxrom -i fallback<br>
flashrom v0.9.4-r1396 on Linux 2.6.35-30-generic (x86_64), built with libpci 3.1.7, GCC 4.4.5, little endian<br>
flashrom is free software, get the source code at <a href="http://www.flashrom.org" target="_blank">http://www.flashrom.org</a><br>
<br>
Using region(s): "normal", "gfxrom", "fallback".<br>
Calibrating delay loop... OK.<br>
Found SST flash chip "SST25VF032B" (4096 kB, SPI) on dummy.<br>
Reading old flash chip contents... done.<br>
Erasing and writing flash chip... Erase/write done.<br>
Verifying flash... VERIFIED.<br></blockquote><div><br></div><div>Looks okay to me. And IIRC -V will list all ROM layout entries found even if they are not included, which is nice for debugging.</div><div><br></div><div>On Mon, Aug 1, 2011 at 6:05 PM, Stefan Tauner <span dir="ltr"><<a href="mailto:stefan.tauner@student.tuwien.ac.at" target="_blank">stefan.tauner@student.tuwien.ac.at</a>></span> wrote: </div>



<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">example of a semi-good run:<br>
flashrom -p dummy:emulate=SST25VF032B -i normal -w ../testimages/4096kB.rand.img  -l test.layout -i gfxrom -i fallback<br>
flashrom v0.9.4-r1396 on Linux 2.6.35-30-generic (x86_64), built with libpci 3.1.7, GCC 4.4.5, little endian<br>
flashrom is free software, get the source code at <a href="http://www.flashrom.org" target="_blank">http://www.flashrom.org</a><br>
<br>
Maximum number of ROM images (4) in layout file reached before end of layout file.<br>
Ignoring the rest of the layout file.<br></blockquote><div><br></div><div>I think that if the user accidentally specifies an invalid layout or number of included regions, then Flashrom should quit. Otherwise Flashrom might not flash everything the user intends, leaving the content in an inconsistent (probably bricked) state. This has become problematic due to complicated ROM layouts in systems where multiple masters use a shared SPI flash (e.g. an EC or Intel's ME insanity).</div>


<div><br></div><div>We can apply this fix in a follow-up patch if you'd prefer:</div><div><br></div><div><div>Index: layout.c</div><div>===================================================================</div><div>--- layout.c    (revision 1402)</div>


<div>+++ layout.c    (working copy)</div><div>@@ -159,7 +159,7 @@</div><div>                                 "file reached before end of layout file.\n",</div><div>                                 MAX_ROMLAYOUT);</div>


<div>                        msg_gerr("Ignoring the rest of the layout file.\n");</div><div>-                       break;</div><div>+                       return 1;</div><div>                }</div><div>                if (2 != fscanf(romlayout, "%s %s\n", tempstr, rom_entries[romimages].name))</div>


<div>                        continue;</div></div><div><br></div><div>On Mon, Aug 1, 2011 at 6:05 PM, Stefan Tauner <span dir="ltr"><<a href="mailto:stefan.tauner@student.tuwien.ac.at" target="_blank">stefan.tauner@student.tuwien.ac.at</a>></span> wrote: </div>



<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">example of a faulty run:<br>
flashrom -p dummy:emulate=SST25VF032B -i normal -w ../testimages/4096kB.rand.img  -l test.layout -i gfxrom -i fallback -i bios<br>
flashrom v0.9.4-r1396 on Linux 2.6.35-30-generic (x86_64), built with libpci 3.1.7, GCC 4.4.5, little endian<br>
flashrom is free software, get the source code at <a href="http://www.flashrom.org" target="_blank">http://www.flashrom.org</a><br>
<br>
Maximum number of ROM images (4) in layout file reached before end of layout file.<br>
Ignoring the rest of the layout file.<br>
Using region(s): "normal", "gfxrom", "fallback"Invalid region specified: "bios"<br></blockquote><div><br></div><div>If you split that last line so that the "Invalid region" part shows up on a newline, it looks okay.</div>



<div><br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Please run "flashrom --help" for usage info.<br>
Signed-off-by: Stefan Tauner <<a href="mailto:stefan.tauner@student.tuwien.ac.at" target="_blank">stefan.tauner@student.tuwien.ac.at</a>><br>
---<br>
 cli_classic.c |   11 ++++-----<br>
 flash.h       |    2 +<br>
 layout.c      |   68 ++++++++++++++++++++++++++++++++++++++++++++++++++------<br>
 3 files changed, 67 insertions(+), 14 deletions(-)<br>
<br>
diff --git a/cli_classic.c b/cli_classic.c<br>
index 36fe9ad..e44ea86 100644<br>
--- a/cli_classic.c<br>
+++ b/cli_classic.c<br>
@@ -233,14 +233,9 @@ int cli_classic(int argc, char *argv[])<br>
                                cli_classic_abort_usage();<br>
                        break;<br>
                case 'i':<br>
-                       /* FIXME: -l has to be specified before -i. */<br>
                        tempstr = strdup(optarg);<br>
-                       if (find_romentry(tempstr) < 0) {<br>
-                               fprintf(stderr, "Error: image %s not found in "<br>
-                                       "layout file or -i specified before "<br>
-                                       "-l\n", tempstr);<br>
+                       if (register_include_arg(tempstr) < 0)<br>
                                cli_classic_abort_usage();<br>
-                       }<br>
                        break;<br>
                case 'L':<br>
                        if (++operation_specified > 1) {<br>
@@ -325,6 +320,10 @@ int cli_classic(int argc, char *argv[])<br>
                cli_classic_abort_usage();<br>
        }<br>
<br>
+       if (process_include_args() < 0) {<br>
+               cli_classic_abort_usage();<br>
+       }<br>
+<br>
        /* FIXME: Print the actions flashrom will take. */<br>
<br>
        if (list_supported) {<br>
diff --git a/flash.h b/flash.h<br>
index 5b49e9d..0848255 100644<br>
--- a/flash.h<br>
+++ b/flash.h<br>
@@ -251,6 +251,8 @@ int print(int type, const char *fmt, ...) __attribute__((format(printf, 2, 3)));<br>
 int cli_classic(int argc, char *argv[]);<br>
<br>
 /* layout.c */<br>
+int register_include_arg(char *name);<br>
+int process_include_args(void);<br>
 int read_romlayout(char *name);<br>
 int find_romentry(char *name);<br>
 int handle_romentries(struct flashchip *flash, uint8_t *oldcontents, uint8_t *newcontents);<br>
diff --git a/layout.c b/layout.c<br>
index d719a05..936e316 100644<br>
--- a/layout.c<br>
+++ b/layout.c<br>
@@ -41,6 +41,12 @@ typedef struct {<br>
        char name[256];<br>
 } romlayout_t;<br>
<br>
+/* include_args lists arguments specified at the command line with -i. They<br>
+ * must be processed at some point so that desired regions are marked as<br>
+ * "included" in the rom_entries list.<br>
+ */<br>
+static char *include_args[MAX_ROMLAYOUT];<br>
+static int num_include_args = 0; /* the number of valid entries. */<br>
 static romlayout_t rom_entries[MAX_ROMLAYOUT];<br>
<br>
 #if CONFIG_INTERNAL == 1 /* FIXME: Move the whole block to cbtable.c? */<br>
@@ -194,6 +200,20 @@ int read_romlayout(char *name)<br>
 }<br>
 #endif<br>
<br>
+/* register an include argument (-i) for later processing */<br>
+int register_include_arg(char *name)<br>
+{<br>
+       if (num_include_args >= MAX_ROMLAYOUT) {<br>
+               msg_gerr("Too many regions included (%i).\n", num_include_args);<br>
+               return -1;<br>
+       }<br>
+<br>
+       include_args[num_include_args] = name;<br>
+       num_include_args++;<br>
+       return num_include_args;<br>
+}<br>
+<br>
+/* returns the index of the entry (or a negative value if it is not found) */<br>
 int find_romentry(char *name)<br>
 {<br>
        int i;<br>
@@ -201,20 +221,49 @@ int find_romentry(char *name)<br>
        if (!romimages)<br>
                return -1;<br>
<br>
-       msg_ginfo("Looking for \"%s\"... ", name);<br>
-<br>
+       msg_gspew("Looking for region \"%s\"... ", name);<br>
        for (i = 0; i < romimages; i++) {<br>
                if (!strcmp(rom_entries[i].name, name)) {<br>
                        rom_entries[i].included = 1;<br>
-                       msg_ginfo("found.\n");<br>
+                       msg_gspew("found.\n");<br>
                        return i;<br>
                }<br>
        }<br>
-       msg_ginfo("not found.\n");      // Not found. Error.<br>
-<br>
+       msg_gspew("not found.\n");<br>
        return -1;<br>
 }<br>
<br>
+/* process -i arguments<br>
+ * returns 0 to indicate success, <0 to indicate failure<br></blockquote><div><br></div><div>I think the usual Flashrom convention is to return 1 to indicate failure. I used <0 out of habit. Let's go ahead and fix that coding convention for upstream if desired, and update the call sites to do error checking correctly.</div>



<div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
+ */<br>
+int process_include_args(void)<br>
+{<br>
+       int i;<br>
+       unsigned int found = 0;<br>
+       for (i = 0; i < num_include_args && include_args[i] != NULL; i++) {<br>
+               /* User has specified an area, but no layout file is loaded. */<br>
+               if (!romimages) {<br>
+                       msg_gerr("Region requested (with -i \"%s\"), "<br>
+                                "but no layout data is available.\n",<br>
+                                include_args[i]);<br>
+                       return -1;<br></blockquote><div><br></div><div>return 1 </div><div><br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
+               }<br>
+<br>
+               if (find_romentry(include_args[i]) < 0) {<br>
+                       msg_gerr("Invalid region specified: \"%s\"\n",<br>
+                                include_args[i]);<br>
+                       return -1;<br></blockquote><div><br></div><div>return 1 </div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
+               }<br>
+               if (found == 0)<br>
+                       msg_ginfo("Using region(s): \"%s\"", include_args[i]);<br>
+               else<br>
+                       msg_ginfo(", \"%s\"", include_args[i]);<br>
+               found++;<br>
+       }<br>
+       msg_ginfo(".\n");<br>
+       return 0;<br>
+}<br>
+<br>
 int find_next_included_romentry(unsigned int start)<br>
 {<br>
        int i;<br>
@@ -246,11 +295,12 @@ int handle_romentries(struct flashchip *flash, uint8_t *oldcontents, uint8_t *ne<br>
        int entry;<br>
        unsigned int size = flash->total_size * 1024;<br>
<br>
-       /* If no layout file was specified or the layout file was empty, assume<br>
-        * that the user wants to flash the complete new image.<br>
+       /* If no regions were specified for inclusion, assume<br>
+        * that the user wants to write the complete new image.<br>
         */<br>
-       if (!romimages)<br>
+       if (num_include_args == 0)<br>
                return 0;<br>
+<br>
        /* Non-included romentries are ignored.<br>
         * The union of all included romentries is used from the new image.<br>
         */<br>
@@ -262,6 +312,8 @@ int handle_romentries(struct flashchip *flash, uint8_t *oldcontents, uint8_t *ne<br>
                               size - start);<br>
                        break;<br>
                }<br>
+<br>
+               /* For non-included region, copy from old content. */<br>
                if (rom_entries[entry].start > start)<br>
                        memcpy(newcontents + start, oldcontents + start,<br>
                               rom_entries[entry].start - start);<br>
<font color="#888888">--<br>
1.7.1<br>
<br>
</font></blockquote></div><br><br clear="all"><br>-- <br>David Hendricks (dhendrix)<br>Systems Software Engineer, Google Inc.<br>