From 2ff978d0c02a7b30c3e431debff2f41eaaf35b05 Mon Sep 17 00:00:00 2001 From: Antoni Date: Fri, 24 Jan 2025 13:16:55 +0100 Subject: [PATCH] copy-blockdev: Make binary fully static This change ensures that `copy-blockdev` binary provided in a delta will run on a system with an older GLib version. hal-api-device dependecy is removed from copy-blockedv. `device_board_set_upgrade_progress_status` is called directly instead. This is done to save us the trouble of linking to hal-* libs statically. Change-Id: I03b7c4d645f1ce68d841f4699c90d8492f5324df --- src/copy-blockdev/CMakeLists.txt | 16 ++++--------- src/copy-blockdev/lib.c | 40 +++++++++++++++++++++++++++----- src/copy-blockdev/lib.h | 2 ++ 3 files changed, 41 insertions(+), 17 deletions(-) diff --git a/src/copy-blockdev/CMakeLists.txt b/src/copy-blockdev/CMakeLists.txt index 6a9d08f..fb9528e 100644 --- a/src/copy-blockdev/CMakeLists.txt +++ b/src/copy-blockdev/CMakeLists.txt @@ -1,13 +1,3 @@ -IF(NOT DEFINE_HOST_BUILD) -SET(CMAKE_C_FLAGS ENV${CFLAGS}) -INCLUDE(FindPkgConfig) -pkg_check_modules(REQUIRED_PKGS REQUIRED hal-api-device) -FOREACH(flag ${REQUIRED_PKGS_CFLAGS}) - SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} ${flag}") -ENDFOREACH(flag) -SET(CMAKE_C_FLAGS ${EXTRA_CFLAGS}) -ENDIF() - set(COPY_BLOCKDEVSRCS ../upgrade-apply/sha1/sha1.c main.c lib.c) include_directories(../upgrade-apply/sha1) set(COPY_BLOCKDEVEXENAME "copy-blockdev") @@ -17,7 +7,11 @@ target_compile_options(${COPY_BLOCKDEVEXENAME} PRIVATE -Wall -Wextra -pedantic - target_compile_definitions(${COPY_BLOCKDEVEXENAME} PRIVATE -D_FILE_OFFSET_BITS=64) IF(NOT DEFINE_HOST_BUILD) -target_link_libraries(${COPY_BLOCKDEVEXENAME} ${REQUIRED_PKGS_LDFLAGS}) +target_link_options(${COPY_BLOCKDEVEXENAME} PRIVATE "-static") +set_target_properties(${COPY_BLOCKDEVEXENAME} PROPERTIES + LINK_SEARCH_START_STATIC ON + LINK_SEARCH_END_STATIC ON + ) ELSE() target_compile_definitions(${COPY_BLOCKDEVEXENAME} PRIVATE -DHOST_BUILD) ENDIF() diff --git a/src/copy-blockdev/lib.c b/src/copy-blockdev/lib.c index 04b7115..bbd6a97 100644 --- a/src/copy-blockdev/lib.c +++ b/src/copy-blockdev/lib.c @@ -15,14 +15,11 @@ #include #include #include +#include #include #include -#ifndef HOST_BUILD -#include -#endif - void usage(char *name) { fprintf(stderr, "Usage:\n" "\n" @@ -244,6 +241,37 @@ void fd_cleanup(int *fd) close(*fd); } +#ifndef HOST_BUILD +static int set_upgrade_progress_status(int progress) +{ + int child_ret = 0; + pid_t pid = fork(); + + if (pid == -1) { + fprintf(stderr, "Failed to fork\n"); + return -1; + } + + if (pid == 0) { + // Max value for progress is 100 + char buf[4]; + snprintf(buf, sizeof(buf), "%d", progress); + if (execl(DEVICE_BOARD_SET_UPGRADE_PROGRESS_STATUS_BIN, + DEVICE_BOARD_SET_UPGRADE_PROGRESS_STATUS_BIN, buf, (char *)NULL) < 0) { + fprintf(stderr, "Failed to exec %s\n", DEVICE_BOARD_SET_UPGRADE_PROGRESS_STATUS_BIN); + exit(EXIT_FAILURE); + } + } + + if (waitpid(pid, &child_ret, 0) < 0) { + fprintf(stderr, "Failed to wait child\n"); + return -1; + } + + return WEXITSTATUS(child_ret) == 0 ? 0 : -1; +} +#endif + int copy_and_update_progress(struct params_t *params) { int prev_progress = params->progress_from; int progress = params->progress_from; @@ -301,7 +329,7 @@ int copy_and_update_progress(struct params_t *params) { if (progress - prev_progress > 0) { #ifndef HOST_BUILD - if (params->update && hal_device_board_set_upgrade_progress_status(progress) < 0) { + if (params->update && set_upgrade_progress_status(progress) < 0) { fprintf(stderr, "Cannot set upgrade progress status.\n"); return ERR_STATUS; }; @@ -313,7 +341,7 @@ int copy_and_update_progress(struct params_t *params) { } #ifndef HOST_BUILD - if (params->update && hal_device_board_set_upgrade_progress_status(params->progress_to) < 0) { + if (params->update && set_upgrade_progress_status(params->progress_to) < 0) { fprintf(stderr, "Cannot set upgrade progress status.\n"); return ERR_STATUS; } diff --git a/src/copy-blockdev/lib.h b/src/copy-blockdev/lib.h index fefb190..3699740 100644 --- a/src/copy-blockdev/lib.h +++ b/src/copy-blockdev/lib.h @@ -26,6 +26,8 @@ #define ERR_SHA1_COMP 14 #define ERR_STATUS 15 +#define DEVICE_BOARD_SET_UPGRADE_PROGRESS_STATUS_BIN "/usr/bin/device_board_set_upgrade_progress_status" + /* * Rationale for using long long values and strtoll instead of * unsigned long long and strtoull to parse ranges which can't -- 2.34.1