Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .bazelrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
build --enable_platform_specific_config
build:linux --define=mtd_backend=true
build:linux --define=spi_backend=true
build:macos --macos_minimum_os=11.0
build --stamp --workspace_status_command '$(pwd)/print_git_commit.sh'
build:asan --copt=-fsanitize=address --linkopt=-fsanitize=address
build:msan --copt=-fsanitize=memory --linkopt=-fsanitize=memory
Expand Down
22 changes: 22 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,25 @@ jobs:
- name: Bazel tests
run: |
bazel test ...

build-macos:
runs-on: 'macos-15'

steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
persist-credentials: false

# No -Werror yet: format strings such as "%lx" for uint64_t and the
# upstream libusb darwin backend both emit warnings on macOS.
- name: Build with Bazel
run: |
bazel build ...

- name: Smoke test htool
run: |
bazel-bin/examples/htool --version

- name: Bazel tests
run: |
bazel test --test_output=errors ...
13 changes: 10 additions & 3 deletions BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,17 @@ cc_library(
":libhoth_transports_headers",
":libhoth_transports_headers_legacy",
"//transports:libhoth_device",
"//transports:libhoth_mtd",
"//transports:libhoth_spi",
"//transports:libhoth_usb",
],
] + select({
"//transports:dbus_backend": ["//transports:libhoth_dbus"],
"//conditions:default": [],
}) + select({
"//transports:mtd_backend": ["//transports:libhoth_mtd"],
"//conditions:default": [],
}) + select({
"//transports:spi_backend": ["//transports:libhoth_spi"],
"//conditions:default": [],
}),
)

alias(
Expand Down
1 change: 1 addition & 0 deletions MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ libusb_ext = use_extension("//:libusb_extension.bzl", "libusb_extension")
use_repo(libusb_ext, "libusb")

bazel_dep(name = "googletest", version = "1.15.2")
bazel_dep(name = "platforms", version = "0.0.11")
bazel_dep(name = "rules_cc", version = "0.1.4")
17 changes: 7 additions & 10 deletions examples/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,6 @@ cc_library(
],
)

config_setting(
name = "dbus_backend",
define_values = {
"dbus_backend": "true",
},
)

cc_library(
name = "htool_cmd",
srcs = ["htool_cmd.c"],
Expand Down Expand Up @@ -275,13 +268,17 @@ cc_binary(
"//protocol:update_session",
"//protocol:util",
"//transports:libhoth_device",
"//transports:libhoth_mtd",
"//transports:libhoth_spi",
"//transports:libhoth_usb",
"//transports:libhoth_usb_device",
"@libusb",
] + select({
":dbus_backend": ["//transports:libhoth_dbus"],
"//transports:dbus_backend": ["//transports:libhoth_dbus"],
"//conditions:default": [],
}) + select({
"//transports:mtd_backend": ["//transports:libhoth_mtd"],
"//conditions:default": [],
}) + select({
"//transports:spi_backend": ["//transports:libhoth_spi"],
"//conditions:default": [],
}),
)
Expand Down
10 changes: 0 additions & 10 deletions examples/htool.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@
#include "protocol/spi_proxy.h"
#include "protocol/util.h"
#include "transports/libhoth_device.h"
#include "transports/libhoth_spi.h"

void htool_report_error(const char* cmd_name, libhoth_error err) {
if (err == HOTH_SUCCESS) {
Expand Down Expand Up @@ -710,15 +709,6 @@ struct libhoth_device* htool_libhoth_device(void) {
return result;
}

int htool_tpm_spi_probe(const struct htool_invocation* inv) {
struct libhoth_device* dev = htool_libhoth_spi_device();
if (!dev) {
return -1;
}

return libhoth_tpm_spi_probe(dev);
}

int htool_external_usb_host_check_presence(const struct htool_invocation* inv) {
struct libhoth_device* dev = htool_libhoth_device();
if (!dev) {
Expand Down
3 changes: 3 additions & 0 deletions examples/htool.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ extern "C" {

void htool_report_error(const char* cmd_name, libhoth_error err);

struct htool_invocation;
struct libhoth_device;

struct libhoth_device* htool_libhoth_dbus_device(void);
Expand All @@ -36,6 +37,8 @@ struct libhoth_device* htool_libhoth_spi_device(void);
struct libhoth_device* htool_libhoth_usb_device(void);
struct libhoth_device* htool_libhoth_device(void);

int htool_tpm_spi_probe(const struct htool_invocation* inv);

#ifdef __cplusplus
}
#endif
Expand Down
16 changes: 14 additions & 2 deletions examples/htool_mtd.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,20 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#include <stdio.h>

#ifdef MTD_BACKEND

#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "../transports/libhoth_mtd.h"
#include "host_commands.h"
#include "htool.h"
#include "htool_cmd.h"
#include "transports/libhoth_mtd.h"

struct libhoth_device* htool_libhoth_mtd_device(void) {
static struct libhoth_device* result;
Expand Down Expand Up @@ -60,3 +63,12 @@ struct libhoth_device* htool_libhoth_mtd_device(void) {
}
return result;
}

#else

struct libhoth_device* htool_libhoth_mtd_device(void) {
fprintf(stderr, "This build doesn't have the MTD backend.\n");
return NULL;
}

#endif // MTD_BACKEND
34 changes: 32 additions & 2 deletions examples/htool_spi.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,20 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#include <stdio.h>

#ifdef SPI_BACKEND

#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "../transports/libhoth_spi.h"
#include "host_commands.h"
#include "htool.h"
#include "htool_cmd.h"
#include "transports/libhoth_spi.h"

struct libhoth_device* htool_libhoth_spi_device(void) {
static struct libhoth_device* result;
Expand Down Expand Up @@ -90,3 +93,30 @@ struct libhoth_device* htool_libhoth_spi_device(void) {
}
return result;
}

int htool_tpm_spi_probe(const struct htool_invocation* inv) {
(void)inv;
struct libhoth_device* dev = htool_libhoth_spi_device();
if (!dev) {
return -1;
}

return libhoth_tpm_spi_probe(dev);
}

#else

#include "htool.h"

struct libhoth_device* htool_libhoth_spi_device(void) {
fprintf(stderr, "This build doesn't have the SPI backend.\n");
return NULL;
}

int htool_tpm_spi_probe(const struct htool_invocation* inv) {
(void)inv;
fprintf(stderr, "This build doesn't have the SPI backend.\n");
return -1;
}

#endif // SPI_BACKEND
2 changes: 1 addition & 1 deletion examples/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ git_version_h = vcs_tag(

incdir = libhoth_include_dirs
link_with = [libhoth.get_static_lib()]
c_args = []
c_args = ['-DMTD_BACKEND', '-DSPI_BACKEND']

if get_option('dbus_backend')
link_with += libhoth_dbus
Expand Down
27 changes: 22 additions & 5 deletions external/libusb.BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,21 @@ cc_library(
"libusb/sync.c",
"libusb/os/events_posix.h",
"libusb/os/events_posix.c",
"libusb/os/linux_usbfs.h",
"libusb/os/linux_usbfs.c",
"libusb/os/linux_netlink.c",
"libusb/os/threads_posix.h",
"libusb/os/threads_posix.c",
"libusb/version.h",
"libusb/version_nano.h",
],
] + select({
"@bazel_tools//src/conditions:darwin": [
"libusb/os/darwin_usb.h",
"libusb/os/darwin_usb.c",
],
"//conditions:default": [
"libusb/os/linux_usbfs.h",
"libusb/os/linux_usbfs.c",
"libusb/os/linux_netlink.c",
],
}),
includes = [
"libusb",
],
Expand All @@ -30,6 +37,16 @@ cc_library(
"-isystem", "external/{}/libusb".format(repo_name()),
"-isystem", "external/{}".format(repo_name()),
],
linkopts = ["-lpthread"],
linkopts = select({
"@bazel_tools//src/conditions:darwin": [
"-lobjc",
"-framework", "IOKit",
"-framework", "CoreFoundation",
"-framework", "Security",
],
"//conditions:default": [
"-lpthread",
],
}),
visibility = ["//visibility:public"],
)
26 changes: 15 additions & 11 deletions external/libusb.patch
Original file line number Diff line number Diff line change
@@ -1,20 +1,11 @@
--- /dev/null 2022-07-08 13:48:05.860119333 -0700
+++ config.h 2022-07-14 11:50:33.978861376 -0700
@@ -0,0 +1,40 @@
@@ -0,0 +1,44 @@
+#define DEFAULT_VISIBILITY __attribute__ ((visibility ("default")))
+#define HAVE_ASM_TYPES_H 1
+#define HAVE_CLOCK_GETTIME 1
+#define HAVE_DECL_EFD_CLOEXEC 1
+#define HAVE_DECL_EFD_NONBLOCK 1
+#define HAVE_DECL_TFD_CLOEXEC 1
+#define HAVE_DECL_TFD_NONBLOCK 1
+#define HAVE_DLFCN_H 1
+#define HAVE_EVENTFD 1
+#define HAVE_INTTYPES_H 1
+#define HAVE_NFDS_T 1
+#define HAVE_PIPE2 1
+#define HAVE_PTHREAD_CONDATTR_SETCLOCK 1
+#define HAVE_PTHREAD_SETNAME_NP 1
+#define HAVE_STDINT_H 1
+#define HAVE_STDIO_H 1
+#define HAVE_STDLIB_H 1
Expand All @@ -23,7 +14,6 @@
+#define HAVE_SYS_STAT_H 1
+#define HAVE_SYS_TIME_H 1
+#define HAVE_SYS_TYPES_H 1
+#define HAVE_TIMERFD 1
+#define HAVE_UNISTD_H 1
+#define LT_OBJDIR ".libs/"
+#define PACKAGE "libusb-1.0"
Expand All @@ -38,6 +28,20 @@
+#define STDC_HEADERS 1
+#define VERSION "1.0.26"
+#define _GNU_SOURCE 1
+#if defined(__APPLE__)
+#define HAVE_PTHREAD_THREADID_NP 1
+#elif defined(__linux__)
+#define HAVE_ASM_TYPES_H 1
+#define HAVE_DECL_EFD_CLOEXEC 1
+#define HAVE_DECL_EFD_NONBLOCK 1
+#define HAVE_DECL_TFD_CLOEXEC 1
+#define HAVE_DECL_TFD_NONBLOCK 1
+#define HAVE_EVENTFD 1
+#define HAVE_PIPE2 1
+#define HAVE_PTHREAD_CONDATTR_SETCLOCK 1
+#define HAVE_PTHREAD_SETNAME_NP 1
+#define HAVE_TIMERFD 1
+#endif
+#ifndef __cplusplus
+/* #undef inline */
+#endif
5 changes: 2 additions & 3 deletions protocol/dfu_hostcmd.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,8 @@
#include "protocol/status.h"

static int generate_random_nonce(struct hoth_dfu_session_id* session_id) {
ssize_t ret = getrandom(&session_id->nonce, sizeof(session_id->nonce), 0);
if (ret == -1) {
perror("getrandom");
if (getentropy(&session_id->nonce, sizeof(session_id->nonce)) != 0) {
perror("getentropy");
return -1;
}
return 0;
Expand Down
26 changes: 26 additions & 0 deletions transports/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,33 @@ cc_library(
hdrs = ["libhoth_ec.h"],
)

config_setting(
name = "dbus_backend",
define_values = {
"dbus_backend": "true",
},
)

config_setting(
name = "mtd_backend",
define_values = {
"mtd_backend": "true",
},
)

config_setting(
name = "spi_backend",
define_values = {
"spi_backend": "true",
},
)

cc_library(
name = "libhoth_mtd",
srcs = ["libhoth_mtd.c"],
hdrs = ["libhoth_mtd.h"],
defines = ["MTD_BACKEND"],
target_compatible_with = ["@platforms//os:linux"],
deps = [
":libhoth_device",
":libhoth_ec",
Expand All @@ -31,6 +54,8 @@ cc_library(
name = "libhoth_spi",
srcs = ["libhoth_spi.c"],
hdrs = ["libhoth_spi.h"],
defines = ["SPI_BACKEND"],
target_compatible_with = ["@platforms//os:linux"],
deps = [
":libhoth_device",
":libhoth_ec",
Expand All @@ -57,6 +82,7 @@ cc_library(
hdrs = ["libhoth_dbus.h"],
defines = ["DBUS_BACKEND"],
linkopts = ["-lsystemd"],
target_compatible_with = ["@platforms//os:linux"],
deps = [
":libhoth_device",
"//protocol:libhoth_status",
Expand Down
Loading