From ff4ff7c8ccf0c5d5c1c363053f0fc84ec5674edf Mon Sep 17 00:00:00 2001 From: Rob Austein Date: Sun, 4 Oct 2015 23:23:07 -0400 Subject: Disallow NULL core argument in lowest-level HAL I/O routines. --- hal_io_i2c.c | 57 ++++++++++++++++++++------------------------------------- 1 file changed, 20 insertions(+), 37 deletions(-) (limited to 'hal_io_i2c.c') diff --git a/hal_io_i2c.c b/hal_io_i2c.c index 3e8ac59..7fb306e 100644 --- a/hal_io_i2c.c +++ b/hal_io_i2c.c @@ -267,24 +267,14 @@ static hal_error_t hal_io_get_read_resp(hal_addr_t offset, uint8_t *data) return HAL_OK; } -static hal_error_t hal_io_get_read_resp_expected(hal_addr_t offset, const uint8_t *data) +hal_error_t hal_io_write(const hal_core_t *core, hal_addr_t offset, const uint8_t *buf, size_t len) { - uint8_t buf[9]; - uint8_t expected[9] = { SOR, READ_OK, (offset >> 8) & 0xff, offset & 0xff, - data[0], data[1], data[2], data[3], EOR }; hal_error_t err; - dump("expect", expected, 9); - - if ((err = hal_io_get_resp(buf, sizeof(buf))) != HAL_OK) - return err; - - return hal_io_compare(buf, expected, sizeof(buf)); -} + if (core == NULL) + return HAL_ERROR_CORE_NOT_FOUND; -hal_error_t hal_io_write(hal_addr_t offset, const uint8_t *buf, size_t len) -{ - hal_error_t err; + offset += hal_core_base(core); for (; len > 0; offset++, buf += 4, len -= 4) if ((err = hal_io_send_write_cmd(offset, buf)) != HAL_OK || @@ -294,43 +284,36 @@ hal_error_t hal_io_write(hal_addr_t offset, const uint8_t *buf, size_t len) return HAL_OK; } -hal_error_t hal_io_read(hal_addr_t offset, uint8_t *buf, size_t len) +hal_error_t hal_io_read(const hal_core_t *core, hal_addr_t offset, uint8_t *buf, size_t len) { hal_error_t err; - for (; len > 0; offset++, buf += 4, len -= 4) - if ((err = hal_io_send_read_cmd(offset)) != HAL_OK || - (err = hal_io_get_read_resp(offset, buf)) != HAL_OK) - return err; + if (core == NULL) + return HAL_ERROR_CORE_NOT_FOUND; - return HAL_OK; -} - -hal_error_t hal_io_expected(hal_addr_t offset, const uint8_t *buf, size_t len) -{ - hal_error_t err; + offset += hal_core_base(core); for (; len > 0; offset++, buf += 4, len -= 4) - if ((err = hal_io_send_read_cmd(offset)) != HAL_OK || - (err = hal_io_get_read_resp_expected(offset, buf)) != HAL_OK) + if ((err = hal_io_send_read_cmd(offset)) != HAL_OK || + (err = hal_io_get_read_resp(offset, buf)) != HAL_OK) return err; return HAL_OK; } -hal_error_t hal_io_init(hal_addr_t offset) +hal_error_t hal_io_init(const hal_core_t *core) { uint8_t buf[4] = { 0, 0, 0, CTRL_INIT }; - return hal_io_write(offset, buf, 4); + return hal_io_write(core, ADDR_CTRL, buf, 4); } -hal_error_t hal_io_next(hal_addr_t offset) +hal_error_t hal_io_next(const hal_core_t *core) { uint8_t buf[4] = { 0, 0, 0, CTRL_NEXT }; - return hal_io_write(offset, buf, 4); + return hal_io_write(core, ADDR_CTRL, buf, 4); } -hal_error_t hal_io_wait(hal_addr_t offset, uint8_t status, int *count) +hal_error_t hal_io_wait(const hal_core_t *core, uint8_t status, int *count) { hal_error_t err; uint8_t buf[4]; @@ -341,7 +324,7 @@ hal_error_t hal_io_wait(hal_addr_t offset, uint8_t status, int *count) if (count && (*count > 0) && (i >= *count)) return HAL_ERROR_IO_TIMEOUT; - if ((err = hal_io_read(offset, buf, 4)) != HAL_OK) + if ((err = hal_io_read(core, ADDR_STATUS, buf, 4)) != HAL_OK) return err; if (buf[3] & status) { @@ -353,16 +336,16 @@ hal_error_t hal_io_wait(hal_addr_t offset, uint8_t status, int *count) } } -hal_error_t hal_io_wait_ready(hal_addr_t offset) +hal_error_t hal_io_wait_ready(const hal_core_t *core) { int limit = 10; - return hal_io_wait(offset, STATUS_READY, &limit); + return hal_io_wait(core, STATUS_READY, &limit); } -hal_error_t hal_io_wait_valid(hal_addr_t offset) +hal_error_t hal_io_wait_valid(const hal_core_t *core) { int limit = 10; - return hal_io_wait(offset, STATUS_VALID, &limit); + return hal_io_wait(core, STATUS_VALID, &limit); } /* -- cgit v1.2.3