diff --git a/examples/htool_payload_update.c b/examples/htool_payload_update.c index 9447218..858c647 100644 --- a/examples/htool_payload_update.c +++ b/examples/htool_payload_update.c @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include "htool_payload_update.h" +#include "examples/htool_payload_update.h" #include #include @@ -24,9 +24,10 @@ #include #include -#include "htool.h" -#include "htool_cmd.h" +#include "examples/htool.h" +#include "examples/htool_cmd.h" #include "protocol/payload_update.h" +#include "protocol/status.h" int htool_payload_update(const struct htool_invocation* inv) { struct libhoth_device* dev = htool_libhoth_device(); @@ -73,30 +74,14 @@ int htool_payload_update(const struct htool_invocation* inv) { goto cleanup; } - enum payload_update_err payload_update_status = libhoth_payload_update( + libhoth_error payload_update_status = libhoth_payload_update( dev, image, statbuf.st_size, skip_erase, binary_file); - switch (payload_update_status) { - case PAYLOAD_UPDATE_OK: - fprintf(stderr, "Payload update finished\n"); - retval = 0; - break; - case PAYLOAD_UPDATE_BAD_IMG: - fprintf(stderr, "Not a valid Titan image.\n"); - break; - case PAYLOAD_UPDATE_INITIATE_FAIL: - fprintf(stderr, "Error when initiating payload update.\n"); - break; - case PAYLOAD_UPDATE_FLASH_FAIL: - fprintf(stderr, "Error when flashing.\n"); - break; - case PAYLOAD_UPDATE_FINALIZE_FAIL: - fprintf(stderr, "Error when finalizing.\n"); - break; - case PAYLOAD_UPDATE_IMAGE_NOT_SECTOR_ALIGNED: - fprintf(stderr, "Payload image is not sector-aligned.\n"); - break; - default: - break; + if (payload_update_status != HOTH_SUCCESS) { + htool_report_error("payload_update", payload_update_status); + retval = -1; + } else { + fprintf(stderr, "Payload update finished\n"); + retval = 0; } int ret = munmap(image, statbuf.st_size); @@ -145,10 +130,14 @@ int htool_payload_read(const struct htool_invocation* inv) { return -1; } - int ret = libhoth_payload_update_read_chunk(dev, fd, length, start); + libhoth_error err = libhoth_payload_update_read_chunk(dev, fd, length, start); close(fd); - return ret; + if (err != HOTH_SUCCESS) { + htool_report_error("payload_update read", err); + return -1; + } + return 0; } const char* payload_update_getstatus_valid_string(uint8_t v) { @@ -185,10 +174,10 @@ int htool_payload_update_getstatus(const struct htool_invocation* inv) { } struct payload_update_status pus; - int ret = libhoth_payload_update_getstatus(dev, &pus); + libhoth_error err = libhoth_payload_update_getstatus(dev, &pus); - if (ret != 0) { - fprintf(stderr, "Failed to get payload update status\n"); + if (err != HOTH_SUCCESS) { + htool_report_error("payload_update getstatus", err); return -1; } @@ -220,9 +209,12 @@ int htool_payload_erase(const struct htool_invocation* inv) { htool_get_param_u32(inv, "length", &length)) { return -1; } - return (libhoth_payload_update_erase(dev, start, length) == PAYLOAD_UPDATE_OK) - ? 0 - : -1; + libhoth_error err = libhoth_payload_update_erase(dev, start, length); + if (err != HOTH_SUCCESS) { + htool_report_error("payload_update erase", err); + return -1; + } + return 0; } int htool_payload_activate(const struct htool_invocation* inv) { @@ -247,9 +239,10 @@ int htool_payload_activate(const struct htool_invocation* inv) { } uint8_t pld_needs_reinitialization = 0; - if (libhoth_payload_update_activate(dev, half, &pld_needs_reinitialization) != - PAYLOAD_UPDATE_OK) { - fprintf(stderr, "Failed to activate payload.\n"); + libhoth_error err = + libhoth_payload_update_activate(dev, half, &pld_needs_reinitialization); + if (err != HOTH_SUCCESS) { + htool_report_error("payload_update activate", err); return -1; } @@ -267,16 +260,16 @@ int htool_payload_update_verify(const struct htool_invocation* inv) { if (htool_get_param_bool(inv, "descriptor", &verify_only_descriptor)) { return -1; } - int ret; + libhoth_error err; if (verify_only_descriptor) { - ret = libhoth_payload_update_verify_descriptor(dev); + err = libhoth_payload_update_verify_descriptor(dev); } else { fprintf(stderr, "Verifying the payload. This can take up to three minutes...\n"); - ret = libhoth_payload_update_verify(dev); + err = libhoth_payload_update_verify(dev); } - if (ret != 0) { - fprintf(stderr, "Payload verify failed\n"); + if (err != HOTH_SUCCESS) { + htool_report_error("payload_update verify", err); return -1; } if (verify_only_descriptor) { @@ -293,15 +286,13 @@ int htool_payload_update_confirm(const struct htool_invocation* inv) { return -1; } - int ret = 0; - - ret = libhoth_payload_update_confirm(dev); - if (ret != 0) { - fprintf(stderr, "Failed to confirm payload update\n"); + libhoth_error err = libhoth_payload_update_confirm(dev); + if (err != HOTH_SUCCESS) { + htool_report_error("payload_update confirm", err); return -1; } - return ret; + return 0; } int htool_payload_update_confirm_get_staged_timeout( @@ -313,9 +304,10 @@ int htool_payload_update_confirm_get_staged_timeout( payload_update_confirm_response_t response = {0}; - int ret = libhoth_payload_update_confirm_get_staged_timeout(dev, &response); - if (ret != 0) { - fprintf(stderr, "Failed to get payload update timeout\n"); + libhoth_error err = + libhoth_payload_update_confirm_get_staged_timeout(dev, &response); + if (err != HOTH_SUCCESS) { + htool_report_error("payload_update confirm_get_staged_timeout", err); return -1; } @@ -334,8 +326,6 @@ int htool_payload_update_confirm_enable(const struct htool_invocation* inv) { return -1; } - int ret = 0; - uint32_t timeout = 0; if (htool_get_param_u32(inv, "timeout", &timeout)) { return -1; @@ -346,11 +336,12 @@ int htool_payload_update_confirm_enable(const struct htool_invocation* inv) { return -1; } - ret = libhoth_payload_update_confirm_enable(dev, enable_confirm, timeout); - if (ret != 0) { - fprintf(stderr, "Failed to confirm payload update\n"); + libhoth_error err = + libhoth_payload_update_confirm_enable(dev, enable_confirm, timeout); + if (err != HOTH_SUCCESS) { + htool_report_error("payload_update confirm_enable", err); return -1; } - return ret; -} \ No newline at end of file + return 0; +} diff --git a/protocol/BUILD b/protocol/BUILD index eeaafd3..6e9e4df 100644 --- a/protocol/BUILD +++ b/protocol/BUILD @@ -141,6 +141,7 @@ cc_test( ], deps = [ ":command_version", + ":libhoth_status", ":payload_info", ":payload_update", "//protocol/test:libhoth_device_mock", diff --git a/protocol/payload_update.c b/protocol/payload_update.c index aa88a8e..99cadd3 100644 --- a/protocol/payload_update.c +++ b/protocol/payload_update.c @@ -12,21 +12,22 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include "payload_update.h" +#include "protocol/payload_update.h" +#include #include #include #include #include #include -#include "command_version.h" -#include "host_cmd.h" -#include "payload_info.h" -#include "progress.h" -#include "status.h" +#include "protocol/command_version.h" +#include "protocol/host_cmd.h" +#include "protocol/payload_info.h" +#include "protocol/progress.h" +#include "protocol/status.h" +#include "protocol/util.h" #include "transports/libhoth_device.h" -#include "util.h" #define PAYLOAD_UPDATE_CONFIRM_OP_ENABLE 0 #define PAYLOAD_UPDATE_CONFIRM_OP_ENABLE_WITH_TIMEOUT 1 @@ -34,25 +35,26 @@ #define PAYLOAD_UPDATE_CONFIRM_OP_CONFIRM 3 #define PAYLOAD_UPDATE_CONFIRM_OP_GET_STAGED_TIMEOUT_VALUES 4 -static int send_payload_update_request_with_command(struct libhoth_device* dev, - uint8_t command) { +static libhoth_error send_payload_update_request_with_command( + struct libhoth_device* dev, uint8_t command) { struct payload_update_packet request; request.type = command; request.offset = 0; request.len = 0; - int ret = libhoth_hostcmd_exec( + libhoth_error ret = libhoth_hostcmd_exec_v2( dev, HOTH_CMD_BOARD_SPECIFIC_BASE + HOTH_PRV_CMD_HOTH_PAYLOAD_UPDATE, 0, &request, sizeof(request), NULL, 0, NULL); - if (ret != 0) { - fprintf(stderr, "Error code from hoth: %d\n", ret); - return -1; + if (ret != HOTH_SUCCESS) { + fprintf(stderr, "Error code from hoth: 0x%016llx\n", + (unsigned long long)ret); + return ret; } - return 0; + return HOTH_SUCCESS; } -static int get_payload_update_version(struct libhoth_device* dev, - uint8_t* version) { +static libhoth_error get_payload_update_version(struct libhoth_device* dev, + uint8_t* version) { uint32_t version_mask = 0; const libhoth_error err = libhoth_get_command_versions( dev, HOTH_CMD_BOARD_SPECIFIC_BASE + HOTH_PRV_CMD_HOTH_PAYLOAD_UPDATE, @@ -63,24 +65,25 @@ static int get_payload_update_version(struct libhoth_device* dev, const bool is_version_0 = (err == HOTH_SUCCESS && (version_mask & 0x2) == 0); if (get_version_unsupported || is_version_0) { *version = 0; - return 0; + return HOTH_SUCCESS; } if (err != HOTH_SUCCESS) { - return -1; + return err; } *version = 1; - return 0; + return HOTH_SUCCESS; } -static int libhoth_payload_update_finalize( +static libhoth_error libhoth_payload_update_finalize( struct libhoth_device* dev, uint8_t* pld_needs_reinitialization) { uint8_t version; - int status = get_payload_update_version(dev, &version); + libhoth_error status = get_payload_update_version(dev, &version); - if (status != 0) { - fprintf(stderr, - "Checking supported command version got unexpected error: %d\n", - status); + if (status != HOTH_SUCCESS) { + fprintf( + stderr, + "Checking supported command version got unexpected error: 0x%016llx\n", + (unsigned long long)status); return status; } @@ -97,34 +100,34 @@ static int libhoth_payload_update_finalize( .type = PAYLOAD_UPDATE_FINALIZE, }; struct payload_update_finalize_response_v1 response = {0}; - status = libhoth_hostcmd_exec( + status = libhoth_hostcmd_exec_v2( dev, HOTH_CMD_BOARD_SPECIFIC_BASE + HOTH_PRV_CMD_HOTH_PAYLOAD_UPDATE, /*version=*/1, &request, sizeof(request), &response, sizeof(response), NULL); - if (status != 0) { + if (status != HOTH_SUCCESS) { return status; } if (pld_needs_reinitialization != NULL) { *pld_needs_reinitialization = response.pld_needs_reinitialization; } - return 0; + return HOTH_SUCCESS; } -static int payload_update_erase_chunk(struct libhoth_device* const dev, - const uint32_t offset, - const uint32_t len) { +static libhoth_error payload_update_erase_chunk( + struct libhoth_device* const dev, const uint32_t offset, + const uint32_t len) { struct payload_update_packet request; request.type = PAYLOAD_UPDATE_ERASE; request.offset = offset; request.len = len; - return libhoth_hostcmd_exec( + return libhoth_hostcmd_exec_v2( dev, HOTH_CMD_BOARD_SPECIFIC_BASE + HOTH_PRV_CMD_HOTH_PAYLOAD_UPDATE, 0, &request, sizeof(request), NULL, 0, NULL); } -enum payload_update_err libhoth_payload_update_erase( - struct libhoth_device* const dev, const uint32_t offset, - const uint32_t len) { +libhoth_error libhoth_payload_update_erase(struct libhoth_device* const dev, + const uint32_t offset, + const uint32_t len) { struct libhoth_progress_stderr erase_progress; libhoth_progress_stderr_init(&erase_progress, "Erase staging side"); @@ -136,19 +139,22 @@ enum payload_update_err libhoth_payload_update_erase( "error: erase length (0x%" PRIx32 ") is zero or not sector-aligned.\n", len); - return PAYLOAD_UPDATE_IMAGE_NOT_SECTOR_ALIGNED; + return LIBHOTH_ERR_CONSTRUCT(HOTH_CTX_CMD_EXEC, HOTH_HOST_SPACE_LIBHOTH, + LIBHOTH_ERR_INVALID_PARAMETER); } if ((offset % sector_erase) != 0) { fprintf(stderr, "error: offset (0x%" PRIx32 ") is not sector-aligned.\n", offset); - return PAYLOAD_UPDATE_IMAGE_NOT_SECTOR_ALIGNED; + return LIBHOTH_ERR_CONSTRUCT(HOTH_CTX_CMD_EXEC, HOTH_HOST_SPACE_LIBHOTH, + LIBHOTH_ERR_INVALID_PARAMETER); } if (UINT32_MAX - offset < len) { fprintf(stderr, "error: invalid erase range (offset 0x%" PRIx32 ", len 0x%" PRIx32 ")\n", offset, len); - return PAYLOAD_UPDATE_INVALID_ARGS; + return LIBHOTH_ERR_CONSTRUCT(HOTH_CTX_CMD_EXEC, HOTH_HOST_SPACE_LIBHOTH, + LIBHOTH_ERR_INVALID_PARAMETER); } uint32_t erased = 0; @@ -160,30 +166,32 @@ enum payload_update_err libhoth_payload_update_erase( const bool send_block_erase = (current_offset % block_erase == 0) && (remaining >= block_erase); const uint32_t chunk_size = send_block_erase ? block_erase : sector_erase; - const int ret = payload_update_erase_chunk(dev, current_offset, chunk_size); - if (ret != 0) { - fprintf(stderr, "error: erase chunk offset 0x%" PRIx32 " err: %d\n", - current_offset, ret); - return PAYLOAD_UPDATE_ERASE_FAIL; + const libhoth_error ret = + payload_update_erase_chunk(dev, current_offset, chunk_size); + if (ret != HOTH_SUCCESS) { + fprintf(stderr, + "error: erase chunk offset 0x%" PRIx32 " err: 0x%016llx\n", + current_offset, (unsigned long long)ret); + return ret; } erased += chunk_size; } erase_progress.progress.func(erase_progress.progress.param, len, len); - return PAYLOAD_UPDATE_OK; + return HOTH_SUCCESS; } -enum payload_update_err libhoth_payload_update(struct libhoth_device* dev, - uint8_t* image, size_t size, - bool skip_erase, - bool binary_file) { +libhoth_error libhoth_payload_update(struct libhoth_device* dev, uint8_t* image, + size_t size, bool skip_erase, + bool binary_file) { if (!binary_file && (libhoth_find_image_descriptor(image, size) == NULL)) { - return PAYLOAD_UPDATE_BAD_IMG; + return LIBHOTH_ERR_CONSTRUCT(HOTH_CTX_CMD_EXEC, HOTH_HOST_SPACE_LIBHOTH, + LIBHOTH_ERR_INVALID_PARAMETER); } if (!skip_erase) { - enum payload_update_err err = libhoth_payload_update_erase(dev, 0, size); - if (err != PAYLOAD_UPDATE_OK) { + libhoth_error err = libhoth_payload_update_erase(dev, 0, size); + if (err != HOTH_SUCCESS) { return err; } } @@ -224,12 +232,13 @@ enum payload_update_err libhoth_payload_update(struct libhoth_device* dev, memcpy(buffer, &request, sizeof(request)); memcpy(buffer + sizeof(request), image + offset, chunk_size); - int ret = libhoth_hostcmd_exec( + libhoth_error ret = libhoth_hostcmd_exec_v2( dev, HOTH_CMD_BOARD_SPECIFIC_BASE + HOTH_PRV_CMD_HOTH_PAYLOAD_UPDATE, 0, buffer, sizeof(request) + chunk_size, NULL, 0, NULL); - if (ret != 0) { - fprintf(stderr, "Error code from hoth: %d\n", ret); - return PAYLOAD_UPDATE_FLASH_FAIL; + if (ret != HOTH_SUCCESS) { + fprintf(stderr, "Error code from hoth: 0x%016llx\n", + (unsigned long long)ret); + return ret; } offset += chunk_size - 1; @@ -242,19 +251,20 @@ enum payload_update_err libhoth_payload_update(struct libhoth_device* dev, if (!binary_file) { fprintf(stderr, "Finalizing payload update.\n"); uint8_t pld_needs_reinitialization = 0; - if (libhoth_payload_update_finalize(dev, &pld_needs_reinitialization) != - 0) { - return PAYLOAD_UPDATE_FINALIZE_FAIL; + libhoth_error finalize_err = + libhoth_payload_update_finalize(dev, &pld_needs_reinitialization); + if (finalize_err != HOTH_SUCCESS) { + return finalize_err; } if (pld_needs_reinitialization != 0) { fprintf(stderr, "PLD updated. Re-initialization needed.\n"); } } - return PAYLOAD_UPDATE_OK; + return HOTH_SUCCESS; } -int libhoth_payload_update_getstatus( +libhoth_error libhoth_payload_update_getstatus( struct libhoth_device* dev, struct payload_update_status* update_status) { struct payload_update_packet request; request.type = PAYLOAD_UPDATE_GET_STATUS; @@ -262,28 +272,31 @@ int libhoth_payload_update_getstatus( request.len = 0; size_t rlen = 0; - int ret = libhoth_hostcmd_exec( + libhoth_error ret = libhoth_hostcmd_exec_v2( dev, HOTH_CMD_BOARD_SPECIFIC_BASE + HOTH_PRV_CMD_HOTH_PAYLOAD_UPDATE, 0, &request, sizeof(request), update_status, sizeof(*update_status), &rlen); - if (ret != 0) { - fprintf(stderr, "HOTH_PAYLOAD_UPDATE_GET_STATUS error code: %d\n", ret); + if (ret != HOTH_SUCCESS) { + fprintf(stderr, "HOTH_PAYLOAD_UPDATE_GET_STATUS error code: 0x%016llx\n", + (unsigned long long)ret); return ret; } if (rlen != sizeof(*update_status)) { fprintf(stderr, - "HOTH_PAYLOAD_UPDATE_GET_STATUS expected exactly %ld response " - "bytes, got %ld\n", + "HOTH_PAYLOAD_UPDATE_GET_STATUS expected exactly %zu response " + "bytes, got %zu\n", sizeof(*update_status), rlen); - return -1; + return LIBHOTH_ERR_CONSTRUCT(HOTH_CTX_CMD_EXEC, HOTH_HOST_SPACE_LIBHOTH, + LIBHOTH_ERR_FAIL); } - return 0; + return HOTH_SUCCESS; } -enum payload_update_err libhoth_payload_update_read_chunk( - struct libhoth_device* dev, int fd, size_t len, size_t offset) { +libhoth_error libhoth_payload_update_read_chunk(struct libhoth_device* dev, + int fd, size_t len, + size_t offset) { const size_t max_chunk_size = LIBHOTH_MAILBOX_SIZE - sizeof(struct hoth_host_response); uint8_t buffer[LIBHOTH_MAILBOX_SIZE]; @@ -298,73 +311,78 @@ enum payload_update_err libhoth_payload_update_read_chunk( pkt.offset = offset; pkt.len = chunk_size; - int ret = libhoth_hostcmd_exec( + libhoth_error ret = libhoth_hostcmd_exec_v2( dev, HOTH_CMD_BOARD_SPECIFIC_BASE + HOTH_PRV_CMD_HOTH_PAYLOAD_UPDATE, 0, - &pkt, sizeof(pkt), &buffer, chunk_size, NULL); + &pkt, sizeof(pkt), buffer, chunk_size, NULL); - if (ret != 0) { - fprintf(stderr, "Payload read failed, err code: %d\n", ret); - return PAYLOAD_UPDATE_READ_FAIL; + if (ret != HOTH_SUCCESS) { + fprintf(stderr, "Payload read failed, err code: 0x%016llx\n", + (unsigned long long)ret); + return ret; } - ret = libhoth_force_write(fd, buffer, chunk_size); - if (ret != 0) { + int write_ret = libhoth_force_write(fd, buffer, chunk_size); + if (write_ret != 0) { fprintf(stderr, "Failed to write payload during payload read, err code: %d\n", - ret); - return PAYLOAD_UPDATE_READ_FAIL; + write_ret); + return LIBHOTH_ERR_CONSTRUCT(HOTH_CTX_CMD_EXEC, HOTH_HOST_SPACE_POSIX, + errno ? errno : EIO); } len -= chunk_size; offset += chunk_size; } - return PAYLOAD_UPDATE_OK; + return HOTH_SUCCESS; } // Version 0 does not return a response. -static enum payload_update_err libhoth_payload_update_activate_v0( +static libhoth_error libhoth_payload_update_activate_v0( struct libhoth_device* dev, struct payload_update_activate_request* request) { - int status = libhoth_hostcmd_exec( + libhoth_error status = libhoth_hostcmd_exec_v2( dev, HOTH_CMD_BOARD_SPECIFIC_BASE + HOTH_PRV_CMD_HOTH_PAYLOAD_UPDATE, /*version=*/0, request, sizeof(*request), NULL, 0, NULL); - if (status != 0) { - fprintf(stderr, "HOTH_PAYLOAD_UPDATE_ACTIVATE v0 error code: %d\n", status); - return PAYLOAD_UPDATE_ACTIVATE_FAIL; + if (status != HOTH_SUCCESS) { + fprintf(stderr, "HOTH_PAYLOAD_UPDATE_ACTIVATE v0 error code: 0x%016llx\n", + (unsigned long long)status); + return status; } - return PAYLOAD_UPDATE_OK; + return HOTH_SUCCESS; } // Version 1 returns a response indicating if the PLD needs to be reinitialized. -static enum payload_update_err libhoth_payload_update_activate_v1( +static libhoth_error libhoth_payload_update_activate_v1( struct libhoth_device* dev, struct payload_update_activate_request* request, uint8_t* pld_needs_reinitialization) { struct payload_update_activate_response_v1 response = {0}; - int status = libhoth_hostcmd_exec( + libhoth_error status = libhoth_hostcmd_exec_v2( dev, HOTH_CMD_BOARD_SPECIFIC_BASE + HOTH_PRV_CMD_HOTH_PAYLOAD_UPDATE, /*version=*/1, request, sizeof(*request), &response, sizeof(response), NULL); - if (status != 0) { - fprintf(stderr, "HOTH_PAYLOAD_UPDATE_ACTIVATE v1 error code: %d\n", status); - return PAYLOAD_UPDATE_ACTIVATE_FAIL; + if (status != HOTH_SUCCESS) { + fprintf(stderr, "HOTH_PAYLOAD_UPDATE_ACTIVATE v1 error code: 0x%016llx\n", + (unsigned long long)status); + return status; } if (pld_needs_reinitialization != NULL) { *pld_needs_reinitialization = response.pld_needs_reinitialization; } - return PAYLOAD_UPDATE_OK; + return HOTH_SUCCESS; } -enum payload_update_err libhoth_payload_update_activate( +libhoth_error libhoth_payload_update_activate( struct libhoth_device* dev, uint8_t half, uint8_t* pld_needs_reinitialization) { uint8_t version; - int status = get_payload_update_version(dev, &version); - if (status != 0) { - fprintf(stderr, - "Checking supported command version got unexpected error: %d\n", - status); - return PAYLOAD_UPDATE_ACTIVATE_FAIL; + libhoth_error status = get_payload_update_version(dev, &version); + if (status != HOTH_SUCCESS) { + fprintf( + stderr, + "Checking supported command version got unexpected error: 0x%016llx\n", + (unsigned long long)status); + return status; } struct payload_update_activate_request request = { @@ -387,16 +405,17 @@ enum payload_update_err libhoth_payload_update_activate( pld_needs_reinitialization); } -int libhoth_payload_update_verify(struct libhoth_device* dev) { +libhoth_error libhoth_payload_update_verify(struct libhoth_device* dev) { return send_payload_update_request_with_command(dev, PAYLOAD_UPDATE_VERIFY); } -int libhoth_payload_update_verify_descriptor(struct libhoth_device* dev) { +libhoth_error libhoth_payload_update_verify_descriptor( + struct libhoth_device* dev) { return send_payload_update_request_with_command( dev, PAYLOAD_UPDATE_VERIFY_DESCRIPTOR); } -int libhoth_payload_update_confirm(struct libhoth_device* dev) { +libhoth_error libhoth_payload_update_confirm(struct libhoth_device* dev) { payload_update_confirm_response_t confirm_response = {0}; payload_update_confirm_request_t confirm_request = {0}; @@ -413,21 +432,22 @@ int libhoth_payload_update_confirm(struct libhoth_device* dev) { memcpy(&send_buf[sizeof(pkt_header)], &confirm_request, sizeof(confirm_request)); - int ret = libhoth_hostcmd_exec( + libhoth_error ret = libhoth_hostcmd_exec_v2( dev, HOTH_CMD_BOARD_SPECIFIC_BASE + HOTH_PRV_CMD_HOTH_PAYLOAD_UPDATE, 0, - &send_buf, sizeof(send_buf), &confirm_response, sizeof(confirm_response), + send_buf, sizeof(send_buf), &confirm_response, sizeof(confirm_response), NULL); - if (ret != 0) { - fprintf(stderr, "Payload update confirm failed, err code: %d\n", ret); - return -1; + if (ret != HOTH_SUCCESS) { + fprintf(stderr, "Payload update confirm failed, err code: 0x%016llx\n", + (unsigned long long)ret); + return ret; } - return 0; + return HOTH_SUCCESS; } -int libhoth_payload_update_confirm_enable(struct libhoth_device* dev, - bool enable, - uint32_t timeout_seconds) { +libhoth_error libhoth_payload_update_confirm_enable(struct libhoth_device* dev, + bool enable, + uint32_t timeout_seconds) { payload_update_confirm_response_t confirm_response = {0}; // Initially fill timeout with the set timeout, if enabled @@ -455,20 +475,21 @@ int libhoth_payload_update_confirm_enable(struct libhoth_device* dev, memcpy(&send_buf[sizeof(pkt_header)], &confirm_request, sizeof(confirm_request)); - int ret = libhoth_hostcmd_exec( + libhoth_error ret = libhoth_hostcmd_exec_v2( dev, HOTH_CMD_BOARD_SPECIFIC_BASE + HOTH_PRV_CMD_HOTH_PAYLOAD_UPDATE, 0, - &send_buf, sizeof(send_buf), &confirm_response, sizeof(confirm_response), + send_buf, sizeof(send_buf), &confirm_response, sizeof(confirm_response), NULL); - if (ret != 0) { - fprintf(stderr, "Payload update confirm enable failed, err code: %d\n", - ret); - return -1; + if (ret != HOTH_SUCCESS) { + fprintf(stderr, + "Payload update confirm enable failed, err code: 0x%016llx\n", + (unsigned long long)ret); + return ret; } - return 0; + return HOTH_SUCCESS; } -int libhoth_payload_update_confirm_get_staged_timeout( +libhoth_error libhoth_payload_update_confirm_get_staged_timeout( struct libhoth_device* dev, payload_update_confirm_response_t* response) { payload_update_confirm_request_t confirm_request = {0}; confirm_request.op = PAYLOAD_UPDATE_CONFIRM_OP_GET_STAGED_TIMEOUT_VALUES; @@ -484,13 +505,14 @@ int libhoth_payload_update_confirm_get_staged_timeout( memcpy(&send_buf[sizeof(pkt_header)], &confirm_request, sizeof(confirm_request)); - int ret = libhoth_hostcmd_exec( + libhoth_error ret = libhoth_hostcmd_exec_v2( dev, HOTH_CMD_BOARD_SPECIFIC_BASE + HOTH_PRV_CMD_HOTH_PAYLOAD_UPDATE, 0, - &send_buf, sizeof(send_buf), response, sizeof(*response), NULL); - if (ret != 0) { - fprintf(stderr, "Payload update get timeout failed, err code: %d\n", ret); - return -1; + send_buf, sizeof(send_buf), response, sizeof(*response), NULL); + if (ret != HOTH_SUCCESS) { + fprintf(stderr, "Payload update get timeout failed, err code: 0x%016llx\n", + (unsigned long long)ret); + return ret; } - return 0; + return HOTH_SUCCESS; } diff --git a/protocol/payload_update.h b/protocol/payload_update.h index ceb8f68..7ec322c 100644 --- a/protocol/payload_update.h +++ b/protocol/payload_update.h @@ -23,6 +23,7 @@ extern "C" { #include #include +#include "protocol/status.h" #include "transports/libhoth_device.h" #define HOTH_PRV_CMD_HOTH_PAYLOAD_UPDATE 0x0005 @@ -116,19 +117,6 @@ static_assert(offsetof(struct payload_update_status, next_half) == 3, static_assert(offsetof(struct payload_update_status, persistent_half) == 4, "Unexpected offset for persistent_half"); -enum payload_update_err { - PAYLOAD_UPDATE_OK = 0, - PAYLOAD_UPDATE_BAD_IMG, - PAYLOAD_UPDATE_INITIATE_FAIL, - PAYLOAD_UPDATE_FLASH_FAIL, - PAYLOAD_UPDATE_FINALIZE_FAIL, - PAYLOAD_UPDATE_READ_FAIL, - PAYLOAD_UPDATE_IMAGE_NOT_SECTOR_ALIGNED, - PAYLOAD_UPDATE_ERASE_FAIL, - PAYLOAD_UPDATE_INVALID_ARGS, - PAYLOAD_UPDATE_ACTIVATE_FAIL, -}; - struct payload_update_packet { uint32_t offset; /* image offset */ uint32_t len; /* packet length excluding this header */ @@ -168,27 +156,27 @@ struct payload_update_activate_response_v1 { uint8_t pld_needs_reinitialization; } __attribute__((packed)); -enum payload_update_err libhoth_payload_update(struct libhoth_device* dev, - uint8_t* image, size_t len, - bool skip_erase, - bool binary_file); -int libhoth_payload_update_getstatus( +libhoth_error libhoth_payload_update(struct libhoth_device* dev, uint8_t* image, + size_t len, bool skip_erase, + bool binary_file); +libhoth_error libhoth_payload_update_getstatus( struct libhoth_device* dev, struct payload_update_status* update_status); -enum payload_update_err libhoth_payload_update_read_chunk( - struct libhoth_device* dev, int fd, size_t len, size_t offset); -enum payload_update_err libhoth_payload_update_erase(struct libhoth_device* dev, - uint32_t offset, - uint32_t len); -enum payload_update_err libhoth_payload_update_activate( +libhoth_error libhoth_payload_update_read_chunk(struct libhoth_device* dev, + int fd, size_t len, + size_t offset); +libhoth_error libhoth_payload_update_erase(struct libhoth_device* dev, + uint32_t offset, uint32_t len); +libhoth_error libhoth_payload_update_activate( struct libhoth_device* dev, uint8_t half, uint8_t* pld_needs_reinitialization); -int libhoth_payload_update_verify(struct libhoth_device* dev); -int libhoth_payload_update_verify_descriptor(struct libhoth_device* dev); -int libhoth_payload_update_confirm(struct libhoth_device* dev); -int libhoth_payload_update_confirm_enable(struct libhoth_device* dev, - bool enable, - uint32_t timeout_seconds); -int libhoth_payload_update_confirm_get_staged_timeout( +libhoth_error libhoth_payload_update_verify(struct libhoth_device* dev); +libhoth_error libhoth_payload_update_verify_descriptor( + struct libhoth_device* dev); +libhoth_error libhoth_payload_update_confirm(struct libhoth_device* dev); +libhoth_error libhoth_payload_update_confirm_enable(struct libhoth_device* dev, + bool enable, + uint32_t timeout_seconds); +libhoth_error libhoth_payload_update_confirm_get_staged_timeout( struct libhoth_device* dev, payload_update_confirm_response_t* timeout_seconds); diff --git a/protocol/payload_update_test.cc b/protocol/payload_update_test.cc index 4de9b47..60b62fd 100644 --- a/protocol/payload_update_test.cc +++ b/protocol/payload_update_test.cc @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include "payload_update.h" +#include "protocol/payload_update.h" #include #include @@ -24,8 +24,9 @@ #include #include -#include "command_version.h" -#include "payload_info.h" +#include "protocol/command_version.h" +#include "protocol/payload_info.h" +#include "protocol/status.h" #include "test/libhoth_device_mock.h" #include "transports/libhoth_device.h" @@ -77,10 +78,10 @@ TEST_F(LibHothTest, payload_update_bad_image_test) { uint8_t bad_buffer[100] = {0}; - EXPECT_EQ(libhoth_payload_update(&hoth_dev_, bad_buffer, sizeof(bad_buffer), + EXPECT_NE(libhoth_payload_update(&hoth_dev_, bad_buffer, sizeof(bad_buffer), /*skip_erase=*/false, /*binary_file=*/false), - PAYLOAD_UPDATE_BAD_IMG); + HOTH_SUCCESS); } TEST_F(LibHothTest, payload_update_test) { @@ -120,7 +121,7 @@ TEST_F(LibHothTest, payload_update_test) { EXPECT_EQ(libhoth_payload_update(&hoth_dev_, buffer.get(), 2 * kAlign, /*skip_erase=*/false, /*binary_file=*/false), - PAYLOAD_UPDATE_OK); + HOTH_SUCCESS); } TEST_F(LibHothTest, payload_update_command_version_unsupported) { @@ -148,7 +149,7 @@ TEST_F(LibHothTest, payload_update_command_version_unsupported) { EXPECT_EQ(libhoth_payload_update(&hoth_dev_, buffer, sizeof(buffer), /*skip_erase=*/true, /*binary_file=*/false), - PAYLOAD_UPDATE_OK); + HOTH_SUCCESS); } TEST_F(LibHothTest, payload_update_erase_fail) { @@ -159,10 +160,10 @@ TEST_F(LibHothTest, payload_update_erase_fail) { uint8_t buffer[4096] = {0}; std::memcpy(buffer, &kMagic, sizeof(kMagic)); - EXPECT_EQ(libhoth_payload_update(&hoth_dev_, buffer, sizeof(buffer), + EXPECT_NE(libhoth_payload_update(&hoth_dev_, buffer, sizeof(buffer), /*skip_erase=*/false, /*binary_file=*/false), - PAYLOAD_UPDATE_ERASE_FAIL); + HOTH_SUCCESS); } TEST_F(LibHothTest, payload_update_flash_fail) { @@ -173,10 +174,10 @@ TEST_F(LibHothTest, payload_update_flash_fail) { uint8_t buffer[100] = {0}; std::memcpy(buffer, &kMagic, sizeof(kMagic)); - EXPECT_EQ(libhoth_payload_update(&hoth_dev_, buffer, sizeof(buffer), + EXPECT_NE(libhoth_payload_update(&hoth_dev_, buffer, sizeof(buffer), /*skip_erase=*/true, /*binary_file=*/false), - PAYLOAD_UPDATE_FLASH_FAIL); + HOTH_SUCCESS); } TEST_F(LibHothTest, payload_update_command_version_fail) { @@ -196,10 +197,10 @@ TEST_F(LibHothTest, payload_update_command_version_fail) { uint8_t buffer[100] = {0}; std::memcpy(buffer, &kMagic, sizeof(kMagic)); - EXPECT_EQ(libhoth_payload_update(&hoth_dev_, buffer, sizeof(buffer), + EXPECT_NE(libhoth_payload_update(&hoth_dev_, buffer, sizeof(buffer), /*skip_erase=*/true, /*binary_file=*/false), - PAYLOAD_UPDATE_FINALIZE_FAIL); + HOTH_SUCCESS); } TEST_F(LibHothTest, payload_update_finalize_fail) { @@ -224,10 +225,10 @@ TEST_F(LibHothTest, payload_update_finalize_fail) { uint8_t buffer[100] = {0}; std::memcpy(buffer, &kMagic, sizeof(kMagic)); - EXPECT_EQ(libhoth_payload_update(&hoth_dev_, buffer, sizeof(buffer), + EXPECT_NE(libhoth_payload_update(&hoth_dev_, buffer, sizeof(buffer), /*skip_erase=*/true, /*binary_file=*/false), - PAYLOAD_UPDATE_FINALIZE_FAIL); + HOTH_SUCCESS); } TEST_F(LibHothTest, payload_update_activate_v0) { @@ -248,7 +249,7 @@ TEST_F(LibHothTest, payload_update_activate_v0) { uint8_t pld_needs_reinit = 0xff; EXPECT_EQ(libhoth_payload_update_activate(&hoth_dev_, 1, &pld_needs_reinit), - PAYLOAD_UPDATE_OK); + HOTH_SUCCESS); EXPECT_EQ(pld_needs_reinit, 0); } @@ -273,7 +274,7 @@ TEST_F(LibHothTest, payload_update_activate_v1) { uint8_t pld_needs_reinit = 0xff; EXPECT_EQ(libhoth_payload_update_activate(&hoth_dev_, 0, &pld_needs_reinit), - PAYLOAD_UPDATE_OK); + HOTH_SUCCESS); EXPECT_EQ(pld_needs_reinit, 1); } @@ -294,8 +295,8 @@ TEST_F(LibHothTest, payload_update_activate_fail) { .WillOnce(DoAll(CopyResp(&kDummy, 0), Return(-1))); uint8_t pld_needs_reinit = 0xff; - EXPECT_EQ(libhoth_payload_update_activate(&hoth_dev_, 1, &pld_needs_reinit), - PAYLOAD_UPDATE_ACTIVATE_FAIL); + EXPECT_NE(libhoth_payload_update_activate(&hoth_dev_, 1, &pld_needs_reinit), + HOTH_SUCCESS); } TEST_F(LibHothTest, payload_update_status) { @@ -310,7 +311,7 @@ TEST_F(LibHothTest, payload_update_status) { .WillOnce(DoAll(CopyResp(&exp_us, sizeof(exp_us)), Return(LIBHOTH_OK))); struct payload_update_status us = {0}; - EXPECT_EQ(libhoth_payload_update_getstatus(&hoth_dev_, &us), LIBHOTH_OK); + EXPECT_EQ(libhoth_payload_update_getstatus(&hoth_dev_, &us), HOTH_SUCCESS); EXPECT_EQ(exp_us.a_valid, us.a_valid); EXPECT_EQ(exp_us.active_half, us.active_half); @@ -328,7 +329,7 @@ TEST_F(LibHothTest, payload_update_verify) { EXPECT_CALL(mock_, receive) .WillOnce(DoAll(CopyResp(&kDummy, 0), Return(LIBHOTH_OK))); - EXPECT_EQ(libhoth_payload_update_verify(&hoth_dev_), 0); + EXPECT_EQ(libhoth_payload_update_verify(&hoth_dev_), HOTH_SUCCESS); } TEST_F(LibHothTest, payload_update_verify_descriptor) { @@ -343,7 +344,7 @@ TEST_F(LibHothTest, payload_update_verify_descriptor) { EXPECT_CALL(mock_, receive) .WillOnce(DoAll(CopyResp(&kDummy, 0), Return(LIBHOTH_OK))); - EXPECT_EQ(libhoth_payload_update_verify_descriptor(&hoth_dev_), 0); + EXPECT_EQ(libhoth_payload_update_verify_descriptor(&hoth_dev_), HOTH_SUCCESS); } TEST_F(LibHothTest, payload_update_erase_test) { @@ -397,7 +398,7 @@ TEST_F(LibHothTest, payload_update_erase_test) { EXPECT_EQ( libhoth_payload_update(&hoth_dev_, buffer, kSize, /*skip_erase=*/false, /*binary_file=*/false), - PAYLOAD_UPDATE_OK); + HOTH_SUCCESS); } TEST_F(LibHothTest, payload_update_test_with_binary_image) { @@ -446,7 +447,7 @@ TEST_F(LibHothTest, payload_update_test_with_binary_image) { EXPECT_EQ(libhoth_payload_update(&hoth_dev_, buffer.get(), 2 * kAlign, /*skip_erase=*/false, /*binary_file=*/true), - PAYLOAD_UPDATE_OK); + HOTH_SUCCESS); } TEST_F(LibHothTest, payload_update_erase_cmd_test) { @@ -484,31 +485,31 @@ TEST_F(LibHothTest, payload_update_erase_cmd_test) { } EXPECT_EQ(libhoth_payload_update_erase(&hoth_dev_, kOffset, kSize), - PAYLOAD_UPDATE_OK); + HOTH_SUCCESS); } TEST_F(LibHothTest, payload_update_erase_cmd_unaligned_offset_test) { constexpr size_t kSize = 4 * 1024; constexpr size_t kOffset = 1; - EXPECT_EQ(libhoth_payload_update_erase(&hoth_dev_, kOffset, kSize), - PAYLOAD_UPDATE_IMAGE_NOT_SECTOR_ALIGNED); + EXPECT_NE(libhoth_payload_update_erase(&hoth_dev_, kOffset, kSize), + HOTH_SUCCESS); } TEST_F(LibHothTest, payload_update_erase_cmd_unaligned_size_test) { constexpr size_t kSize = 4 * 1024 + 1; constexpr size_t kOffset = 4 * 1024; - EXPECT_EQ(libhoth_payload_update_erase(&hoth_dev_, kOffset, kSize), - PAYLOAD_UPDATE_IMAGE_NOT_SECTOR_ALIGNED); + EXPECT_NE(libhoth_payload_update_erase(&hoth_dev_, kOffset, kSize), + HOTH_SUCCESS); } TEST_F(LibHothTest, payload_update_erase_cmd_zero_size_test) { constexpr size_t kSize = 0; constexpr size_t kOffset = 0; - EXPECT_EQ(libhoth_payload_update_erase(&hoth_dev_, kOffset, kSize), - PAYLOAD_UPDATE_IMAGE_NOT_SECTOR_ALIGNED); + EXPECT_NE(libhoth_payload_update_erase(&hoth_dev_, kOffset, kSize), + HOTH_SUCCESS); } TEST_F(LibHothTest, payload_update_erase_cmd_range_overflow_test) { @@ -517,8 +518,8 @@ TEST_F(LibHothTest, payload_update_erase_cmd_range_overflow_test) { constexpr uint32_t kOffset = 0xFFFFF000; constexpr uint32_t kSize = 2 * kSectorErase; - EXPECT_EQ(libhoth_payload_update_erase(&hoth_dev_, kOffset, kSize), - PAYLOAD_UPDATE_INVALID_ARGS); + EXPECT_NE(libhoth_payload_update_erase(&hoth_dev_, kOffset, kSize), + HOTH_SUCCESS); } TEST_F(LibHothTest, payload_update_read_chunk_test) { @@ -541,7 +542,7 @@ TEST_F(LibHothTest, payload_update_read_chunk_test) { } guard = {fd}; EXPECT_EQ(libhoth_payload_update_read_chunk(&hoth_dev_, fd, len, offset), - PAYLOAD_UPDATE_OK); + HOTH_SUCCESS); ASSERT_EQ(lseek(fd, 0, SEEK_SET), 0); uint8_t actual_data[sizeof(expected_data)] = {0};