Fix hal APIs according to documentations 82/280682/1
authorSangYoun Kwak <sy.kwak@samsung.com>
Fri, 2 Sep 2022 06:35:04 +0000 (15:35 +0900)
committerSangYoun Kwak <sy.kwak@samsung.com>
Fri, 2 Sep 2022 06:36:30 +0000 (15:36 +0900)
Documents of hal APIs can be found here: hal-api-device/include/hal-board.h

Change-Id: I3c0dff62ba8ca8360c417b8c032bbdd880c80f48
Signed-off-by: SangYoun Kwak <sy.kwak@samsung.com>
hw/board/board.c

index 1ddd57c2601ba324bec42b3e7175b99d65288085..53fda635a79b1ad93067cef1640a94a4c0b05c7a 100644 (file)
 #define PARTITION_AB_PATH        INFORM_MNT_PATH"/partition-ab.info"
 #define PARTITION_AB_CLONED_PATH INFORM_MNT_PATH"/partition-ab-cloned.info"
 #define UPGRADE_STATUS_PATH      INFORM_MNT_PATH"/upgrade-status.info"
-#define SERIAL_FILE_PATH         "/sys/firmware/devicetree/base/serial-number"
 #define PARTITION_A_STATUS_PATH  INFORM_MNT_PATH"/partition-a-status.info"
 #define PARTITION_B_STATUS_PATH  INFORM_MNT_PATH"/partition-b-status.info"
 #define REBOOT_PARAM_PATH        INFORM_MNT_PATH"/reboot-param.bin"
-
-#define INT_BUFFER_LEN    32
-#define STATUS_BUFFER_LEN 16
-#define LINE_LEN          64
+#define SERIAL_FILE_PATH         "/sys/firmware/devicetree/base/serial-number"
 
 #define PARTITION_STATUS_OK        "ok"
 #define PARTITION_STATUS_CORRUPTED "corrupted"
 #define PARTITION_STATUS_FAILED    "failed"
+#define BOOT_MODE_NORMAL           "norm"
 
 static int get_device_serial_number(char *buffer, int len)
 {
@@ -65,6 +62,35 @@ static int get_device_serial_number(char *buffer, int len)
        return 0;
 }
 
+static int clear_boot_mode(void)
+{
+       /* clear contents of the file first */
+       if (truncate(REBOOT_PARAM_PATH, 0) == -1) {
+               int ferror = errno;
+               errno = 0;
+               return -ferror;
+       }
+
+       return sys_set_str(REBOOT_PARAM_PATH, BOOT_MODE_NORMAL);
+}
+
+static int get_boot_mode(char *buffer, const int max_len)
+{
+       char scan_format[32] = { 0, };
+       int ret = 0;
+
+       snprintf(scan_format, sizeof(scan_format), "bootmode=%%%ds", max_len - 1);
+
+       ret = libsys_parse_cmdline_scanf(scan_format, buffer);
+
+       if (ret <= 0) {
+               return -ENOENT;
+       }
+
+       return 0;
+}
+
+
 static int get_current_partition(char *partition_ab)
 {
        int ret_parse = libsys_parse_cmdline_scanf("partition_ab=%c", partition_ab);
@@ -107,22 +133,6 @@ static int switch_partition(char partition_ab)
        return sys_write_buf(PARTITION_AB_PATH, next_partition_ab);
 }
 
-static int get_boot_mode(char *buffer, const int max_len)
-{
-       char scan_format[32] = { 0, };
-       int ret = 0;
-
-       snprintf(scan_format, sizeof(scan_format), "bootmode=%%%ds", max_len - 1);
-
-       ret = libsys_parse_cmdline_scanf(scan_format, buffer);
-
-       if (ret <= 0) {
-               return -ENOENT;
-       }
-
-       return 0;
-}
-
 static int set_partition_ab_cloned(void)
 {
        return sys_write_buf(PARTITION_AB_CLONED_PATH, "1");
@@ -135,61 +145,45 @@ static int clear_partition_ab_cloned(void)
 
 static int get_partition_ab_cloned(int *cloned)
 {
-       return sys_get_int(PARTITION_AB_CLONED_PATH, cloned);
-}
-
-static int set_upgrade_status(int status)
-{
-       int fd;
-       int ferror;
-       char buf[INT_BUFFER_LEN];
-       int buf_strlen;
-       int written_len;
-
-       buf_strlen = snprintf(buf, sizeof(buf), "%d", status);
-
-       /* file should be truncated before writing status to it */
-       errno = 0;
-       fd = open(UPGRADE_STATUS_PATH, O_WRONLY | O_TRUNC);
-       if (fd < 0) {
-               ferror = errno;
-               errno = 0;
-               return -ferror;
-       }
+       int ret_get_int = sys_get_int(PARTITION_AB_CLONED_PATH, cloned);
 
-       errno = 0;
-       written_len = write(fd, buf, buf_strlen);
-       if (written_len < 0) {
-               ferror = errno;
-               errno = 0;
-               close(fd);
-               return -ferror;
+       if (ret_get_int < 0) {
+               return ret_get_int;
        }
 
-       close(fd);
-
        return 0;
 }
 
-static int get_upgrade_status(int *status)
-{
-       return sys_get_int(UPGRADE_STATUS_PATH, status);
-}
-
 static int set_partition_status(char partition_ab, const char *status)
 {
        char *path;
+       char target_partition_ab = partition_ab;
+
+       if (target_partition_ab == '\0') {
+               int ret_cur_part = get_current_partition(&target_partition_ab);
+
+               if (ret_cur_part < 0) {
+                       return ret_cur_part;
+               }
 
-       if ((partition_ab != 'a' && partition_ab != 'b') || status == NULL)
-               return -ENOTSUP;
-       if (strcmp(status, PARTITION_STATUS_OK) != 0 && strcmp(status, PARTITION_STATUS_CORRUPTED) != 0 &&
-               strcmp(status, PARTITION_STATUS_FAILED) != 0)
-               return -ENOTSUP;
+               if (target_partition_ab != 'a' && target_partition_ab != 'b') {
+                       return -ENOENT;
+               }
+       }
 
-       if (partition_ab == 'a')
+       if (target_partition_ab == 'a') {
                path = PARTITION_A_STATUS_PATH;
-       else
+       } else if (target_partition_ab == 'b') {
                path = PARTITION_B_STATUS_PATH;
+       } else {
+               return -EINVAL;
+       }
+
+       if (status == NULL || (strcmp(status, PARTITION_STATUS_OK) != 0 &&
+                               strcmp(status, PARTITION_STATUS_CORRUPTED) != 0 &&
+                               strcmp(status, PARTITION_STATUS_FAILED) != 0)) {
+               return -EINVAL;
+       }
 
        /* clear contents of the file first */
        if (truncate(path, 0) == -1) {
@@ -201,43 +195,79 @@ static int set_partition_status(char partition_ab, const char *status)
        return sys_set_str(path, (char *)status);
 }
 
-static int get_partition_status(char partition_ab, char *res_buf, const int max_len)
+static int get_partition_status(char partition_ab, char *buffer, const int max_len)
 {
        char *path;
+       char target_partition_ab = partition_ab;
+
+       if (target_partition_ab == '\0') {
+               int ret_cur_part = get_current_partition(&target_partition_ab);
+
+               if (ret_cur_part < 0) {
+                       return ret_cur_part;
+               }
 
-       if (partition_ab != 'a' && partition_ab != 'b')
-               return -ENOTSUP;
-       if (res_buf == NULL || max_len <= strlen(PARTITION_STATUS_CORRUPTED) + 1)
-               return -ENOBUFS;
+               if (target_partition_ab != 'a' && target_partition_ab != 'b') {
+                       return -ENOENT;
+               }
+       }
 
-       if (partition_ab == 'a')
+       if (target_partition_ab == 'a') {
                path = PARTITION_A_STATUS_PATH;
-       else
-               path = PARTITION_B_STATUS_PATH;
+       } else if (target_partition_ab == 'b') {
+               path = PARTITION_A_STATUS_PATH;
+       } else {
+               return -EINVAL;
+       }
+
+       int res_get_str = sys_get_str(path, buffer, max_len);
+       if (res_get_str < 0) {
+               return res_get_str;
+       }
 
-       int result = sys_get_str(path, res_buf, max_len);
        /* remove potential newline | tokenizer will place '\0' in a place of '\n' */
-       strtok(res_buf, "\n");
+       strtok(buffer, "\n");
 
-       if (strcmp(res_buf, PARTITION_STATUS_OK) != 0 && strcmp(res_buf, PARTITION_STATUS_CORRUPTED) != 0 && strcmp(res_buf, PARTITION_STATUS_FAILED) != 0)
-               result = -ENOTSUP;
+       if (strcmp(buffer, PARTITION_STATUS_OK) != 0 &&
+                       strcmp(buffer, PARTITION_STATUS_CORRUPTED) != 0 &&
+                       strcmp(buffer, PARTITION_STATUS_FAILED) != 0) {
+               return -ENOENT;
+       }
 
-       return result;
+       return 0;
 }
 
-static int clear_boot_mode()
+static int set_upgrade_status(int status)
 {
-       char *cont = "norm";
-       char *path = REBOOT_PARAM_PATH;
+       /* status: -1 if upgrade fail, otherwise 0~100 */
+       if (status < -1 || status > 100) {
+               return -EINVAL;
+       }
 
        /* clear contents of the file first */
-       if (truncate(path, 0) == -1) {
+       if (truncate(UPGRADE_STATUS_PATH, 0) == -1) {
                int ferror = errno;
                errno = 0;
                return -ferror;
        }
 
-       return sys_set_str(path, cont);
+       return sys_set_int(UPGRADE_STATUS_PATH, status);
+}
+
+static int get_upgrade_status(int *status)
+{
+       int ret_get_int = sys_get_int(UPGRADE_STATUS_PATH, status);
+
+       if (ret_get_int < 0) {
+               return ret_get_int;
+       }
+
+       /* status: -1 if upgrade fail, otherwise 0~100 */
+       if (*status < -1 || *status > 100) {
+               return -EINVAL;
+       }
+
+       return 0;
 }
 
 static int board_init(void **data)
@@ -249,18 +279,20 @@ static int board_init(void **data)
                return -ENOMEM;
 
        board_funcs->get_device_serial_number  = get_device_serial_number;
+
+       board_funcs->clear_boot_mode           = clear_boot_mode;
        board_funcs->get_boot_mode             = get_boot_mode;
+
        board_funcs->get_current_partition     = get_current_partition;
        board_funcs->switch_partition          = switch_partition;
        board_funcs->set_partition_ab_cloned   = set_partition_ab_cloned;
        board_funcs->clear_partition_ab_cloned = clear_partition_ab_cloned;
        board_funcs->get_partition_ab_cloned   = get_partition_ab_cloned;
+       board_funcs->set_partition_status      = set_partition_status;
+       board_funcs->get_partition_status      = get_partition_status;
 
        board_funcs->set_upgrade_status        = set_upgrade_status;
        board_funcs->get_upgrade_status        = get_upgrade_status;
-       board_funcs->set_partition_status      = set_partition_status;
-       board_funcs->get_partition_status      = get_partition_status;
-       board_funcs->clear_boot_mode           = clear_boot_mode;
 
        *data = (void *)board_funcs;