Various places in the flashrom source feature custom parameter extraction from programmer_param
This led to wildly differing syntax for programmer parameters, and
it also voids pretty much every assumption you could make about
programmer_param. The latter is a problem for libflashrom.
Use extract_param everywhere, clean up related code and make it more
foolproof. Add two instances of exit(1) where we have no option to
return an error. Remove six instances of exit(1) where returning an
error was possible.
WARNING: This changes programmer parameter syntax for a few programmers!
Corresponding to flashrom svn r1070.
Signed-off-by: Carl-Daniel Hailfinger <c-d.hailfinger.devel.2006@gmx.net>
Acked-by: Michael Karcher <flashrom@mkarcher.dialup.fu-berlin.de>
diff --git a/flashrom.c b/flashrom.c
index 090b424..db15ae5 100644
--- a/flashrom.c
+++ b/flashrom.c
@@ -434,6 +434,7 @@
int programmer_init(char *param)
{
+ int ret;
/* Initialize all programmer specific data. */
/* Default to unlimited decode sizes. */
max_rom_decode = (const struct decode_sizes) {
@@ -456,7 +457,13 @@
programmer_param = param;
msg_pdbg("Initializing %s programmer\n",
programmer_table[programmer].name);
- return programmer_table[programmer].init();
+ ret = programmer_table[programmer].init();
+ if (programmer_param && strlen(programmer_param)) {
+ msg_perr("Unhandled programmer parameters: %s\n",
+ programmer_param);
+ /* Do not error out here, the init itself was successful. */
+ }
+ return ret;
}
int programmer_shutdown(void)
@@ -564,22 +571,24 @@
char *strcat_realloc(char *dest, const char *src)
{
dest = realloc(dest, strlen(dest) + strlen(src) + 1);
- if (!dest)
+ if (!dest) {
+ msg_gerr("Out of memory!\n");
return NULL;
+ }
strcat(dest, src);
return dest;
}
/* This is a somewhat hacked function similar in some ways to strtok().
- * It will look for needle in haystack, return a copy of needle and remove
- * everything from the first occurrence of needle to the next delimiter
- * from haystack.
+ * It will look for needle with a subsequent '=' in haystack, return a copy of
+ * needle and remove everything from the first occurrence of needle to the next
+ * delimiter from haystack.
*/
char *extract_param(char **haystack, char *needle, char *delim)
{
- char *param_pos, *rest, *tmp;
- char *dev = NULL;
- int devlen;
+ char *param_pos, *opt_pos, *rest;
+ char *opt = NULL;
+ int optlen;
int needlelen;
needlelen = strlen(needle);
@@ -595,43 +604,41 @@
do {
if (!param_pos)
return NULL;
- /* Beginning of the string? */
- if (param_pos == *haystack)
- break;
- /* After a delimiter? */
- if (strchr(delim, *(param_pos - 1)))
- break;
+ /* Needle followed by '='? */
+ if (param_pos[needlelen] == '=') {
+
+ /* Beginning of the string? */
+ if (param_pos == *haystack)
+ break;
+ /* After a delimiter? */
+ if (strchr(delim, *(param_pos - 1)))
+ break;
+ }
/* Continue searching. */
param_pos++;
param_pos = strstr(param_pos, needle);
} while (1);
-
+
if (param_pos) {
- param_pos += strlen(needle);
- devlen = strcspn(param_pos, delim);
- if (devlen) {
- dev = malloc(devlen + 1);
- if (!dev) {
- msg_gerr("Out of memory!\n");
- exit(1);
- }
- strncpy(dev, param_pos, devlen);
- dev[devlen] = '\0';
- }
- rest = param_pos + devlen;
- rest += strspn(rest, delim);
- param_pos -= strlen(needle);
- memmove(param_pos, rest, strlen(rest) + 1);
- tmp = realloc(*haystack, strlen(*haystack) + 1);
- if (!tmp) {
+ /* Get the string after needle and '='. */
+ opt_pos = param_pos + needlelen + 1;
+ optlen = strcspn(opt_pos, delim);
+ /* Return an empty string if the parameter was empty. */
+ opt = malloc(optlen + 1);
+ if (!opt) {
msg_gerr("Out of memory!\n");
exit(1);
}
- *haystack = tmp;
+ strncpy(opt, opt_pos, optlen);
+ opt[optlen] = '\0';
+ rest = opt_pos + optlen;
+ /* Skip all delimiters after the current parameter. */
+ rest += strspn(rest, delim);
+ memmove(param_pos, rest, strlen(rest) + 1);
+ /* We could shrink haystack, but the effort is not worth it. */
}
-
- return dev;
+ return opt;
}
/* start is an offset to the base address of the flash chip */