From 54b0d629ca2c3f3dd77ec53d249c863e14d9fd70 Mon Sep 17 00:00:00 2001 From: SangYoun Kwak Date: Thu, 12 Sep 2024 20:22:38 +0900 Subject: [PATCH] osu: Add board parameter setters Setter for board parameters using hal apis are added. * hal_device_board_switch_partition * hal_device_board_switch_partition * hal_device_board_clear_partition_ab_cloned * hal_device_board_set_partition_ab_cloned * hal_device_board_set_partition_status * hal_device_board_set_upgrade_progress_status * hal_device_board_set_upgrade_type * hal_device_board_set_upgrade_state Change-Id: I13d39b210349766d76fadee3679dff337c6b2689 Signed-off-by: SangYoun Kwak --- tools/osu/CMakeLists.txt | 11 +- tools/osu/board-params-getter.c | 23 +--- tools/osu/board-params-setter.c | 219 ++++++++++++++++++++++++++++++++ tools/osu/board-params-setter.h | 19 +++ tools/osu/board-params-util.c | 46 +++++++ tools/osu/board-params-util.h | 20 +++ tools/osu/board-params.h | 1 + tools/osu/osu.c | 13 ++ 8 files changed, 330 insertions(+), 22 deletions(-) create mode 100644 tools/osu/board-params-setter.c create mode 100644 tools/osu/board-params-setter.h create mode 100644 tools/osu/board-params-util.c create mode 100644 tools/osu/board-params-util.h diff --git a/tools/osu/CMakeLists.txt b/tools/osu/CMakeLists.txt index 6790f7a..c5e805a 100644 --- a/tools/osu/CMakeLists.txt +++ b/tools/osu/CMakeLists.txt @@ -10,12 +10,19 @@ INCLUDE(FindPkgConfig) pkg_check_modules(REQUIRED_PKGS REQUIRED ${PKG_MODULES}) FOREACH(flag ${REQUIRED_PKGS_CFLAGS}) - SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} ${flag}") + SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} ${flag}") ENDFOREACH(flag) INCLUDE_DIRECTORIES(. ${CMAKE_SOURCE_DIR}/include) -FILE(GLOB_RECURSE SRCS osu.c resize.c update.c board-params-getter.c) +FILE(GLOB_RECURSE SRCS + osu.c + resize.c + update.c + board-params-getter.c + board-params-setter.c + board-params-util.c +) ADD_EXECUTABLE(${OUTPUT_BIN_NAME} ${SRCS}) SET(CMAKE_EXE_LINKER_FLAGS "-Wl,--as-needed -pie") TARGET_LINK_LIBRARIES(${OUTPUT_BIN_NAME} ${REQUIRED_PKGS_LDFLAGS} update-control) diff --git a/tools/osu/board-params-getter.c b/tools/osu/board-params-getter.c index 4fb98f9..425099b 100644 --- a/tools/osu/board-params-getter.c +++ b/tools/osu/board-params-getter.c @@ -6,6 +6,9 @@ #include +#include "board-params-getter.h" +#include "board-params-util.h" + #define BOARD_PARAM_PARAMS_LEN 2 struct board_param_getter { @@ -175,26 +178,6 @@ static void print_board_param_getters(void) " * e.g. \"partition-status,a\"\n"); } -static size_t split_params(char **params, size_t params_len, const char *delim, char *params_str) -{ - assert(params_len > 0); - - char *strtok_saveptr = NULL; - size_t params_index = 0; - - params[0] = strtok_r(params_str, delim, &strtok_saveptr); - if (params[0] == NULL) - return 0; - - for (params_index = 1; params_index < params_len; ++params_index) { - params[params_index] = strtok_r(NULL, delim, &strtok_saveptr); - if (params[params_index] == NULL) - break; - } - - return params_index; -} - int do_get_board_param(const char *params_str) { int ret = 0; diff --git a/tools/osu/board-params-setter.c b/tools/osu/board-params-setter.c new file mode 100644 index 0000000..1ea762b --- /dev/null +++ b/tools/osu/board-params-setter.c @@ -0,0 +1,219 @@ +#include +#include +#include +#include +#include +#include + +#include + +#include "board-params-setter.h" +#include "board-params-util.h" + +#define BOARD_PARAM_PARAMS_LEN 3 + +struct board_param_setter { + char *param_name; + char *description; + int (*setter)(char **params, size_t params_len); +}; + +static int partition_ab_setter(char **params, size_t params_len) +{ + assert(params != NULL); + + if (params_len != 1) + return -EINVAL; + + if (strncmp(params[0], "toggle", sizeof("toggle")) == 0) + return hal_device_board_switch_partition('\0'); + + char partition_ab = params[0][0]; + + return hal_device_board_switch_partition(partition_ab); +} + +static int partition_ab_cloned_setter(char **params, size_t params_len) +{ + assert(params != NULL); + + if (params_len != 1) + return -EINVAL; + + if (strncmp(params[0], "0", sizeof("0")) == 0) + return hal_device_board_clear_partition_ab_cloned(); + + if (strncmp(params[0], "1", sizeof("1")) == 0) + return hal_device_board_set_partition_ab_cloned(); + + return -EINVAL; +} + +static int partition_status_setter(char **params, size_t params_len) +{ + assert(params != NULL); + + if (params_len != 2) + return -EINVAL; + + char partition_ab = params[0][0]; + char *status = params[1]; + + return hal_device_board_set_partition_status(partition_ab, status); +} + +static int upgrade_progress_status_setter(char **params, size_t params_len) +{ + assert(params != NULL); + + if (params_len != 1) + return -EINVAL; + + int upgrade_progress_status = 0; + + if (parse_integer(params[0], &upgrade_progress_status) < 0) + return -EINVAL; + + return hal_device_board_set_upgrade_progress_status(upgrade_progress_status); +} + +static int upgrade_type_setter(char **params, size_t params_len) +{ + assert(params != NULL); + + if (params_len != 1) + return -EINVAL; + + char *upgrade_type = params[0]; + + return hal_device_board_set_upgrade_type(upgrade_type); +} + +static int upgrade_state_setter(char **params, size_t params_len) +{ + assert(params != NULL); + + if (params_len != 2) + return -EINVAL; + + char *state_from = params[0]; + char *state_to = params[1]; + + return hal_device_board_set_upgrade_state(state_from, state_to); +} + +static struct board_param_setter setters[] = { + { + .param_name = "partition-ab", + .description = "Switch partition between a/b. " + "1 param is required: ", + .setter = partition_ab_setter + }, { + .param_name = "partition-ab-cloned", + .description = "Set cloned flag 0 or 1. " + "1 param is required: <0 or 1>", + .setter = partition_ab_cloned_setter + }, { + .param_name = "partition-status", + .description = "Set partition status. " + "2 params are required: ,", + .setter = partition_status_setter + }, { + .param_name = "upgrade-progress-status", + .description = "Set upgrade progress status. 1 integer is requied: <-1 ~ 100>", + .setter = upgrade_progress_status_setter + }, { + .param_name = "upgrade-type", + .description = "Set upgrade type. 1 param is required: ", + .setter = upgrade_type_setter + }, { + .param_name = "upgrade-state", + .description = "Set upgrade state. 2 params are required.", + .setter = upgrade_state_setter + }, { + /* sentinel for this array */ + .param_name = NULL, + .description = NULL, + .setter = NULL + }, +}; + +static int get_board_param_setter(const char *param_name, struct board_param_setter **board_param_setter) +{ + size_t param_name_len = strlen(param_name); + + for (int i = 0; setters[i].param_name != NULL; ++i) { + if (strncmp(setters[i].param_name, param_name, param_name_len + 1) == 0) { + *board_param_setter = &setters[i]; + return 0; + } + } + + return -1; +} + +static void print_board_param_setters(void) +{ + printf("Available board params:\n"); + for (int i = 0; setters[i].param_name != NULL; ++i) { + if (setters[i].setter == NULL) + continue; + printf(" %s: %s\n", setters[i].param_name, setters[i].description); + } + + printf("\n" + " * Parameters should be provided separated with ','.\n" + " * e.g. \"upgrade_progress_status,100\"\n"); +} + +int do_set_board_param(const char *params_str) +{ + int ret = 0; + char *params_str_dup = NULL; + struct board_param_setter *board_param_setter = NULL; + + char *params[BOARD_PARAM_PARAMS_LEN] = { NULL, }; + size_t params_len = BOARD_PARAM_PARAMS_LEN; + char *param_name = NULL; + + params_str_dup = strdup(params_str); + if (params_str_dup == NULL) { + printf("Failed to allocate memory for params.\n"); + return 1; + } + + params_len = split_params(params, params_len, ",", params_str_dup); + if (params_len < 1) { + printf("Invalid parameter.\n"); + ret = -1; + goto release_and_exit; + } + param_name = params[0]; + + if (strncmp(param_name, "help", sizeof("help")) == 0) { + print_board_param_setters(); + ret = 0; + goto release_and_exit; + } + + ret = get_board_param_setter(param_name, &board_param_setter); + if (ret != 0) { + printf("Invalid parameter(s): %s\n", param_name); + print_board_param_setters(); + goto release_and_exit; + } + + ret = (board_param_setter->setter)(params + 1, params_len - 1); + if (ret != 0) { + printf("Failed to set board param %s: %d\n", param_name, ret); + printf("%s: %s\n", param_name, board_param_setter->description); + goto release_and_exit; + } + + printf("Success\n"); + +release_and_exit: + free(params_str_dup); + + return ret; +} diff --git a/tools/osu/board-params-setter.h b/tools/osu/board-params-setter.h new file mode 100644 index 0000000..84cc3e2 --- /dev/null +++ b/tools/osu/board-params-setter.h @@ -0,0 +1,19 @@ +/* + * Copyright (c) 2024 Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the License); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an AS IS BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#pragma once + +int do_set_board_param(const char *param_name); diff --git a/tools/osu/board-params-util.c b/tools/osu/board-params-util.c new file mode 100644 index 0000000..edf1f32 --- /dev/null +++ b/tools/osu/board-params-util.c @@ -0,0 +1,46 @@ +#include +#include +#include +#include +#include + +size_t split_params(char **params, size_t params_len, const char *delim, char *params_str) +{ + assert(params_len > 0); + + char *strtok_saveptr = NULL; + size_t params_index = 0; + + params[0] = strtok_r(params_str, delim, &strtok_saveptr); + if (params[0] == NULL) + return 0; + + for (params_index = 1; params_index < params_len; ++params_index) { + params[params_index] = strtok_r(NULL, delim, &strtok_saveptr); + if (params[params_index] == NULL) + break; + } + + return params_index; +} + +int parse_integer(const char *str, int *val) +{ + char *endptr = NULL; + long longval = 0; + + errno = 0; + longval = strtol(str, &endptr, 10); + + if (errno != 0 || endptr[0] != '\0') { + errno = 0; + return -EINVAL; + } + + if (longval > INT_MAX || longval < INT_MIN) + return -EINVAL; + + *val = (int)longval; + + return 0; +} diff --git a/tools/osu/board-params-util.h b/tools/osu/board-params-util.h new file mode 100644 index 0000000..1510d97 --- /dev/null +++ b/tools/osu/board-params-util.h @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2024 Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the License); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an AS IS BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#pragma once + +size_t split_params(char **params, size_t params_len, const char *delim, char *params_str); +int parse_integer(const char *str, int *val); diff --git a/tools/osu/board-params.h b/tools/osu/board-params.h index 9bd7d78..6a65c2c 100644 --- a/tools/osu/board-params.h +++ b/tools/osu/board-params.h @@ -17,3 +17,4 @@ #pragma once #include "board-params-getter.h" +#include "board-params-setter.h" diff --git a/tools/osu/osu.c b/tools/osu/osu.c index 6ff9ab5..73f3218 100644 --- a/tools/osu/osu.c +++ b/tools/osu/osu.c @@ -34,6 +34,7 @@ struct arguments { bool update; bool online; bool get_board_param; + bool set_board_param; char *board_param; }; @@ -49,6 +50,7 @@ static int parse_args(int argc, char **argv, struct arguments *args) args->update = false; args->online = false; args->get_board_param = false; + args->set_board_param = false; args->board_param = NULL; struct option long_options[] = { @@ -57,6 +59,7 @@ static int parse_args(int argc, char **argv, struct arguments *args) {"offline-update", no_argument, NULL, 'f'}, {"online-update", no_argument, NULL, 'n'}, {"get-board-param", required_argument, NULL, 'g'}, + {"set-board-param", required_argument, NULL, 's'}, {"help", no_argument, NULL, 'h'}, {} }; @@ -83,6 +86,11 @@ static int parse_args(int argc, char **argv, struct arguments *args) args->board_param = optarg; return 0; } + case 's': { + args->set_board_param = true; + args->board_param = optarg; + return 0; + } case 'h': { args->help = true; return 0; @@ -114,6 +122,8 @@ static void print_help(char *my_name) " After that, performing the OS Upgrade will be impossible.\n" " --get-board-param [,...] Print value of a board param\n" " Use as 'help' for details.\n" + " --set-board-param [,...] Set value of a board param\n" + " Use as 'help' for details.\n" "\n" ,my_name); } @@ -139,6 +149,9 @@ int main(int argc, char **argv) if (args.get_board_param) return do_get_board_param(args.board_param); + if (args.set_board_param) + return do_set_board_param(args.board_param); + print_help(argv[0]); return 1; -- 2.34.1