diff options
author | Rob Austein <sra@hactrn.net> | 2015-05-20 16:57:33 -0400 |
---|---|---|
committer | Rob Austein <sra@hactrn.net> | 2015-05-20 16:57:33 -0400 |
commit | fa13a8485ce07aecfbfa481baccfc33565f0be0d (patch) | |
tree | 48d7da997e617fbbcb22bf86f96a3a43ca7049ce /hal_io_eim.c | |
parent | 93941c6393ec3454c64528e537534284a81b04ac (diff) |
Add csprng and hash modules. Add real error codes instead of magic
numbers, and propegate error codes up from lower layers. Whack
C++-isms, add parenthesese to a lot of macro expressions for
evaluation safety, other minor cleanup.
Diffstat (limited to 'hal_io_eim.c')
-rw-r--r-- | hal_io_eim.c | 65 |
1 files changed, 37 insertions, 28 deletions
diff --git a/hal_io_eim.c b/hal_io_eim.c index ebffd24..6f9e159 100644 --- a/hal_io_eim.c +++ b/hal_io_eim.c @@ -49,18 +49,19 @@ static int inited = 0; /* ---------------- EIM low-level code ---------------- */ -static int init(void) +static hal_error_t init(void) { if (inited) - return 0; + return HAL_OK; if (eim_setup() != 0) { - fprintf(stderr, "EIM setup failed\n"); - return -1; + if (debug) + fprintf(stderr, "[ EIM setup failed ]\n"); + return HAL_ERROR_IO_SETUP_FAILED; } inited = 1; - return 0; + return HAL_OK; } /* translate cryptech register number to EIM address @@ -95,10 +96,12 @@ static void dump(char *label, const uint8_t *buf, size_t len) } } -int hal_io_write(off_t offset, const uint8_t *buf, size_t len) +hal_error_t hal_io_write(off_t offset, const uint8_t *buf, size_t len) { - if (init() != 0) - return -1; + hal_error_t err; + + if ((err = init()) != HAL_OK) + return err; dump("write ", buf, len); @@ -109,16 +112,17 @@ int hal_io_write(off_t offset, const uint8_t *buf, size_t len) eim_write_32(offset, &val); } - return 0; + return HAL_OK; } -int hal_io_read(off_t offset, uint8_t *buf, size_t len) +hal_error_t hal_io_read(off_t offset, uint8_t *buf, size_t len) { uint8_t *rbuf = buf; int rlen = len; + hal_error_t err; - if (init() != 0) - return -1; + if ((err = init()) != HAL_OK) + return err; offset = eim_offset(offset); for (; rlen > 0; offset += 4, rbuf += 4, rlen -= 4) { @@ -129,79 +133,84 @@ int hal_io_read(off_t offset, uint8_t *buf, size_t len) dump("read ", buf, len); - return 0; + return HAL_OK; } -int hal_io_expected(off_t offset, const uint8_t *expected, size_t len) +hal_error_t hal_io_expected(off_t offset, const uint8_t *expected, size_t len) { + hal_error_t err; uint8_t *buf; int i; buf = malloc(len); if (buf == NULL) { perror("malloc"); - return 1; + return HAL_ERROR_MEMORY; } dump("expect", expected, len); - if (hal_io_read(offset, buf, len) != 0) + if ((err = hal_io_read(offset, buf, len)) != HAL_OK) goto errout; - for (i = 0; i < len; ++i) + for (i = 0; i < len; ++i) { if (buf[i] != expected[i]) { fprintf(stderr, "response byte %d: expected 0x%02x, got 0x%02x\n", i, expected[i], buf[i]); + err = HAL_ERROR_IO_UNEXPECTED; goto errout; } + } free(buf); - return 0; + return HAL_OK; + errout: free(buf); - return 1; + return err; } -int hal_io_init(off_t offset) +hal_error_t hal_io_init(off_t offset) { uint8_t buf[4] = { 0, 0, 0, CTRL_INIT }; return hal_io_write(offset, buf, 4); } -int hal_io_next(off_t offset) +hal_error_t hal_io_next(off_t offset) { uint8_t buf[4] = { 0, 0, 0, CTRL_NEXT }; return hal_io_write(offset, buf, 4); } -int hal_io_wait(off_t offset, uint8_t status, int *count) +hal_error_t hal_io_wait(off_t offset, uint8_t status, int *count) { + hal_error_t err; uint8_t buf[4]; int i; for (i = 1; ; ++i) { if (count && (*count > 0) && (i >= *count)) { fprintf(stderr, "hal_io_wait timed out\n"); - return 1; + return HAL_ERROR_IO_TIMEOUT; } - if (hal_io_read(offset, buf, 4) != 0) - return -1; + if ((err = hal_io_read(offset, buf, 4)) != HAL_OK) + return err; if (buf[3] & status) { if (count) *count = i; - return 0; + return HAL_OK; } } } -int hal_io_wait_ready(off_t offset) +hal_error_t hal_io_wait_ready(off_t offset) { int limit = 256; return hal_io_wait(offset, STATUS_READY, &limit); } -int hal_io_wait_valid(off_t offset) +hal_error_t hal_io_wait_valid(off_t offset) { int limit = 256; return hal_io_wait(offset, STATUS_VALID, &limit); |