diff --git a/BUILD b/BUILD index fb7df78..4749c97 100644 --- a/BUILD +++ b/BUILD @@ -23,7 +23,7 @@ genrule( cc_library( name = "libhoth_transports_headers", hdrs = [ - "//transports:headers", + "//transports:headers", ], include_prefix = "libhoth/transports", strip_include_prefix = "transports", @@ -32,7 +32,7 @@ cc_library( cc_library( name = "libhoth_transports_headers_legacy", hdrs = [ - "//transports:headers", + "//transports:headers", ], include_prefix = "transports", strip_include_prefix = "transports", @@ -40,15 +40,15 @@ cc_library( cc_library( name = "libhoth", + visibility = ["//visibility:public"], deps = [ - "//transports:libhoth_device", - "//transports:libhoth_usb", - "//transports:libhoth_spi", - "//transports:libhoth_mtd", ":libhoth_transports_headers", ":libhoth_transports_headers_legacy", + "//transports:libhoth_device", + "//transports:libhoth_mtd", + "//transports:libhoth_spi", + "//transports:libhoth_usb", ], - visibility = ["//visibility:public"], ) alias( diff --git a/examples/BUILD b/examples/BUILD index 31df79f..2cfe8c8 100644 --- a/examples/BUILD +++ b/examples/BUILD @@ -195,9 +195,9 @@ cc_binary( "htool_jtag.h", "htool_key_rotation.c", "htool_key_rotation.h", + "htool_macros.h", "htool_mauv.c", "htool_mauv.h", - "htool_macros.h", "htool_mtd.c", "htool_panic.c", "htool_panic.h", @@ -231,10 +231,10 @@ cc_binary( "htool_spi.c", "htool_statistics.c", "htool_statistics.h", - "htool_tpm.c", - "htool_tpm.h", "htool_target_control.c", "htool_target_control.h", + "htool_tpm.c", + "htool_tpm.h", "htool_update_failure_reasons.h", "htool_usb.c", "htool_usb.h", @@ -247,11 +247,11 @@ cc_binary( "//protocol:authz_record", "//protocol:chipinfo", "//protocol:console", - "//protocol:gpio_drive_strength", "//protocol:controlled_storage", "//protocol:dfu_check", "//protocol:dfu_hostcmd", "//protocol:firmware_update", + "//protocol:gpio_drive_strength", "//protocol:hello", "//protocol:host_cmd", "//protocol:i2c", diff --git a/examples/htool.c b/examples/htool.c index 3e1b02f..ade53de 100644 --- a/examples/htool.c +++ b/examples/htool.c @@ -16,6 +16,7 @@ #include #include +#include #include #include #include @@ -893,6 +894,59 @@ static int command_set_gpio_drive_strength(const struct htool_invocation* inv) { return 0; } +static int command_get_gpio_drive_strength(const struct htool_invocation* inv) { + struct libhoth_device* dev = htool_libhoth_device(); + if (!dev) { + return -1; + } + + bool has_dio = htool_has_param(inv, "dio"); + bool has_mio = htool_has_param(inv, "mio"); + + if (!has_dio && !has_mio) { + fprintf(stderr, "Must specify either --dio or --mio.\n"); + return -1; + } + + if (has_dio && has_mio) { + fprintf(stderr, "Cannot specify both --dio and --mio.\n"); + return -1; + } + + uint32_t pad; + uint8_t dio_index = 0; + uint8_t mio_index = 0; + if (has_dio) { + uint32_t dio; + if (htool_get_param_u32(inv, "dio", &dio)) { + return -1; + } + dio_index = (uint8_t)dio; + pad = dio + LIBHOTH_GPIO_DIO_PAD_OFFSET; + } else { + uint32_t mio; + if (htool_get_param_u32(inv, "mio", &mio)) { + return -1; + } + mio_index = (uint8_t)mio; + pad = mio; + } + + uint8_t strength = 0; + libhoth_error err = + libhoth_get_gpio_drive_strength(dev, (uint8_t)pad, &strength); + if (err != HOTH_SUCCESS) { + htool_report_error("get_gpio_drive_strength", err); + return -1; + } + if (has_dio) { + printf("DIO %" PRIu8 " drive strength: %" PRIu8 "\n", dio_index, strength); + } else { + printf("MIO %" PRIu8 " drive strength: %" PRIu8 "\n", mio_index, strength); + } + return 0; +} + static int command_hello(const struct htool_invocation* inv) { struct libhoth_device* dev = htool_libhoth_device(); if (!dev) { @@ -1750,6 +1804,37 @@ static const struct htool_cmd CMDS[] = { {}}, .func = command_set_gpio_drive_strength, }, + { + .verbs = (const char*[]){"gpio", "get_drive_strength", NULL}, + .desc = "Get GPIO drive strength", + .params = + (const struct htool_param[]){ + {.type = HTOOL_FLAG_VALUE, + .ch = 'd', + .name = "dio", + .default_value = NULL, + .desc = "The DIO pad with the given index. Values are:\n" + " 0 => USB_DP\n" + " 1 => USB_DN\n" + " 2-5 => SPI_HOST0_D0-3\n" + " 6-9 => SPI_DEV_D0-3\n" + " 12 => SPI_DEV_CLK\n" + " 13 => SPI_DEV_CSB\n" + " 14 => SPI_HOST0_CLK\n" + " 15 => SPI_HOST0_CSB"}, + {.type = HTOOL_FLAG_VALUE, + .ch = 'm', + .name = "mio", + .default_value = NULL, + .desc = "The MIO pad with the given index. Values are:\n" + " 0-8 => IOA0-8\n" + " 9-21 => IOB0-12\n" + " 22-34 => IOC0-12\n" + " 35-41 => IOR0-7\n" + " 42-46 => IOR10-13"}, + {}}, + .func = command_get_gpio_drive_strength, + }, { .verbs = (const char*[]){"hello", NULL}, .desc = "A test function to send and receive an integer", diff --git a/protocol/BUILD b/protocol/BUILD index ef1d1b5..5094ad2 100644 --- a/protocol/BUILD +++ b/protocol/BUILD @@ -8,10 +8,10 @@ cc_library( srcs = ["status.c"], hdrs = ["status.h"], defines = ["LIBHOTH_SUPPORT_LIBUSB"], + visibility = ["//visibility:public"], deps = [ - "@libusb//:libusb", + "@libusb", ], - visibility = ["//visibility:public"], ) cc_test( @@ -125,10 +125,10 @@ cc_library( deps = [ ":command_version", ":host_cmd", + ":libhoth_status", ":payload_info", ":progress", ":util", - ":libhoth_status", "//transports:libhoth_device", ], ) @@ -583,8 +583,8 @@ cc_test( "//protocol/test:test_data", ], deps = [ - ":host_cmd", ":dfu_check", + ":host_cmd", ":opentitan_version", "//protocol/test:libhoth_device_mock", "//transports:libhoth_device", @@ -593,7 +593,6 @@ cc_test( ], ) - cc_library( name = "util", srcs = ["util.c"], @@ -621,4 +620,3 @@ cc_test( "@googletest//:gtest_main", ], ) - diff --git a/protocol/gpio_drive_strength.c b/protocol/gpio_drive_strength.c index e463dd1..cbf2a76 100644 --- a/protocol/gpio_drive_strength.c +++ b/protocol/gpio_drive_strength.c @@ -35,3 +35,25 @@ libhoth_error libhoth_set_gpio_drive_strength(struct libhoth_device* const dev, /*version=*/0, &request, sizeof(request), /*response=*/NULL, /*response_size=*/0, NULL); } + +libhoth_error libhoth_get_gpio_drive_strength(struct libhoth_device* const dev, + const uint8_t pad, + uint8_t* const strength) { + if (strength == NULL) { + return LIBHOTH_ERR_CONSTRUCT(HOTH_CTX_CMD_EXEC, HOTH_HOST_SPACE_LIBHOTH, + LIBHOTH_ERR_INVALID_PARAMETER); + } + + const struct hoth_request_get_gpio_drive_strength request = { + .pad = pad, + }; + struct hoth_response_get_gpio_drive_strength response; + const libhoth_error err = libhoth_hostcmd_exec_v2( + dev, HOTH_CMD_GET_GPIO_DRIVE_STRENGTH, /*version=*/0, &request, + sizeof(request), &response, sizeof(response), /*out_resp_size=*/NULL); + if (err != HOTH_SUCCESS) { + return err; + } + *strength = response.strength; + return HOTH_SUCCESS; +} diff --git a/protocol/gpio_drive_strength.h b/protocol/gpio_drive_strength.h index 90c1dc2..590f083 100644 --- a/protocol/gpio_drive_strength.h +++ b/protocol/gpio_drive_strength.h @@ -25,6 +25,7 @@ extern "C" { #endif #define HOTH_CMD_SET_GPIO_DRIVE_STRENGTH 0x3E56 +#define HOTH_CMD_GET_GPIO_DRIVE_STRENGTH 0x3E59 #define MAX_GPIO_DRIVE_STRENGTH 0xF #define LIBHOTH_GPIO_DIO_PAD_OFFSET 128 @@ -33,9 +34,20 @@ struct hoth_request_set_gpio_drive_strength { uint8_t strength; } __hoth_align1; +struct hoth_request_get_gpio_drive_strength { + uint8_t pad; +} __hoth_align1; + +struct hoth_response_get_gpio_drive_strength { + uint8_t strength; +} __hoth_align1; + libhoth_error libhoth_set_gpio_drive_strength(struct libhoth_device* dev, uint8_t pad, uint8_t strength); +libhoth_error libhoth_get_gpio_drive_strength(struct libhoth_device* dev, + uint8_t pad, uint8_t* strength); + #ifdef __cplusplus } #endif diff --git a/protocol/gpio_drive_strength_test.cc b/protocol/gpio_drive_strength_test.cc index 22306d1..7f43c34 100644 --- a/protocol/gpio_drive_strength_test.cc +++ b/protocol/gpio_drive_strength_test.cc @@ -41,3 +41,27 @@ TEST_F(LibHothTest, set_gpio_drive_strength_invalid_param) { EXPECT_EQ(LIBHOTH_ERR_GET_SPACE(err), HOTH_HOST_SPACE_LIBHOTH); EXPECT_EQ(LIBHOTH_ERR_GET_CODE(err), LIBHOTH_ERR_INVALID_PARAMETER); } + +TEST_F(LibHothTest, get_gpio_drive_strength_success) { + EXPECT_CALL(mock_, send(_, UsesCommand(HOTH_CMD_GET_GPIO_DRIVE_STRENGTH), _)) + .WillOnce(Return(LIBHOTH_OK)); + + struct hoth_response_get_gpio_drive_strength resp = { + .strength = 7, + }; + EXPECT_CALL(mock_, receive) + .WillOnce(DoAll(CopyResp(&resp, sizeof(resp)), Return(LIBHOTH_OK))); + + uint8_t strength = 0; + EXPECT_EQ(libhoth_get_gpio_drive_strength(&hoth_dev_, 10, &strength), + HOTH_SUCCESS); + EXPECT_EQ(strength, 7); +} + +TEST_F(LibHothTest, get_gpio_drive_strength_null_param) { + libhoth_error err = libhoth_get_gpio_drive_strength(&hoth_dev_, 10, nullptr); + EXPECT_NE(err, HOTH_SUCCESS); + EXPECT_EQ(LIBHOTH_ERR_GET_CTX(err), HOTH_CTX_CMD_EXEC); + EXPECT_EQ(LIBHOTH_ERR_GET_SPACE(err), HOTH_HOST_SPACE_LIBHOTH); + EXPECT_EQ(LIBHOTH_ERR_GET_CODE(err), LIBHOTH_ERR_INVALID_PARAMETER); +} diff --git a/transports/BUILD b/transports/BUILD index cad5f25..1e58321 100644 --- a/transports/BUILD +++ b/transports/BUILD @@ -78,6 +78,6 @@ cc_library( ) filegroup( - name = "headers", - srcs = glob(["*.h"]) + name = "headers", + srcs = glob(["*.h"]), )