Adapted two callers to handle return codes instead of relying on the exit call. Rest of upstream callers are prepared to handle return values.
Signed-off-by: Niklas Söderlund niso@kth.se --- serprog.c | 34 ++++++++++++++++++++++------------ 1 file changed, 22 insertions(+), 12 deletions(-)
diff --git a/serprog.c b/serprog.c index f03d332..aac10af 100644 --- a/serprog.c +++ b/serprog.c @@ -246,17 +246,23 @@ static int sp_docommand(uint8_t command, uint32_t parmlen, unsigned char c; if (sp_automatic_cmdcheck(command)) return 1; - if (write(sp_fd, &command, 1) != 1) - sp_die("Error: cannot write op code"); - if (write(sp_fd, params, parmlen) != (parmlen)) - sp_die("Error: cannot write parameters"); - if (read(sp_fd, &c, 1) != 1) - sp_die("Error: cannot read from device"); + if (write(sp_fd, &command, 1) != 1) { + msg_perr("Error: cannot write op code: %s\n", strerror(errno)); + return 1; + } + if (write(sp_fd, params, parmlen) != (parmlen)) { + msg_perr("Error: cannot write parameters: %s\n", strerror(errno)); + return 1; + } + if (read(sp_fd, &c, 1) != 1) { + msg_perr("Error: cannot read from device: %s\n", strerror(errno)); + return 1; + } if (c == S_NAK) return 1; if (c != S_ACK) { - msg_perr("Error: invalid response 0x%02X from device\n",c); - exit(1); + msg_perr("Error: invalid response 0x%02X from device\n", c); + return 1; } if (retlen) { int rd_bytes = 0; @@ -264,8 +270,10 @@ static int sp_docommand(uint8_t command, uint32_t parmlen, int r; r = read(sp_fd, retparms + rd_bytes, retlen - rd_bytes); - if (r <= 0) - sp_die("Error: cannot read return parameters"); + if (r <= 0) { + msg_perr("Error: cannot read return parameters: %s\n", strerror(errno)); + return 1; + } rd_bytes += r; } while (rd_bytes != retlen); } @@ -491,7 +499,8 @@ int serprog_init(void) the programmer to tell us its limits, but if it doesn't, we will assume stuff, so it's in the programmers best interest to tell us. */ - sp_docommand(S_CMD_S_BUSTYPE, 1, &bt, 0, NULL); + if (sp_docommand(S_CMD_S_BUSTYPE, 1, &bt, 0, NULL)) + return 1; if (!sp_docommand(S_CMD_Q_WRNMAXLEN, 0, NULL, 3, rbuf)) { uint32_t v; v = ((unsigned int)(rbuf[0]) << 0); @@ -513,7 +522,8 @@ int serprog_init(void) msg_pdbg(MSGHEADER "Maximum read-n length is %d\n", v); } bt = serprog_buses_supported; - sp_docommand(S_CMD_S_BUSTYPE, 1, &bt, 0, NULL); + if (sp_docommand(S_CMD_S_BUSTYPE, 1, &bt, 0, NULL)) + return 1; }
if (serprog_buses_supported & BUS_NONSPI) {
On Wed, 6 Jun 2012 10:18:00 +0200 Niklas Söderlund niso@kth.se wrote:
@@ -491,7 +499,8 @@ int serprog_init(void) the programmer to tell us its limits, but if it doesn't, we will assume stuff, so it's in the programmers best interest to tell us. */
sp_docommand(S_CMD_S_BUSTYPE, 1, &bt, 0, NULL);
if (sp_docommand(S_CMD_S_BUSTYPE, 1, &bt, 0, NULL))
if (!sp_docommand(S_CMD_Q_WRNMAXLEN, 0, NULL, 3, rbuf)) { uint32_t v; v = ((unsigned int)(rbuf[0]) << 0);return 1;
@@ -513,7 +522,8 @@ int serprog_init(void) msg_pdbg(MSGHEADER "Maximum read-n length is %d\n", v); } bt = serprog_buses_supported;
sp_docommand(S_CMD_S_BUSTYPE, 1, &bt, 0, NULL);
if (sp_docommand(S_CMD_S_BUSTYPE, 1, &bt, 0, NULL))
}return 1;
those calls actually change the behavior, because it was previously ok if they fail (which is arguably correct), i decided to change them anyway because the if before guards older correct implementations against this anyway. i just moved the first one above the comment because it would contradict it otherwise.
thanks! Acked-by: Stefan Tauner stefan.tauner@student.tuwien.ac.at and applied in r1557