From 585c26054a9d04417e2a0aa9d37df823ee4e0f60 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=AB=98=E7=A5=A5?= Date: Wed, 23 Apr 2025 09:35:55 +0000 Subject: [PATCH 1/4] VAB MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 高祥 --- .../innerkits/fs_manager/libfs_hvb/fs_hvb.c | 13 +++++ .../innerkits/fs_manager/libfs_hvb/hvb_ops.c | 57 +++++++++++++++++++ .../fs_manager/libfs_hvb/include/fs_hvb.h | 3 +- test/unittest/BUILD.gn | 5 ++ 4 files changed, 77 insertions(+), 1 deletion(-) diff --git a/interfaces/innerkits/fs_manager/libfs_hvb/fs_hvb.c b/interfaces/innerkits/fs_manager/libfs_hvb/fs_hvb.c index 4ec40a207..9e4a6f3fe 100755 --- a/interfaces/innerkits/fs_manager/libfs_hvb/fs_hvb.c +++ b/interfaces/innerkits/fs_manager/libfs_hvb/fs_hvb.c @@ -321,6 +321,19 @@ static int FsHvbGetCert(struct hvb_cert *cert, const char *devName, struct hvb_v struct hvb_cert_data *p = vd->certs; struct hvb_cert_data *end = p + vd->num_loaded_certs; + int bootSlots = GetBootSlots(); + if (bootSlots > 1) { + if (devNameLen <= FS_HVB_AB_SUFFIX_LEN) { + BEGET_LOGE("error, devname (%s) is invlaid, devnamelen = %u", devName, devNameLen); + return -1; + } + if (memcmp(devName + devNameLen - FS_HVB_AB_SUFFIX_LEN, "_a", FS_HVB_AB_SUFFIX_LEN) == 0 || + memcmp(devName + devNameLen - FS_HVB_AB_SUFFIX_LEN, "_b", FS_HVB_AB_SUFFIX_LEN) == 0) { + BEGET_LOGI("remove ab suffix in %s to match in hvb certs", devName); + devNameLen -= FS_HVB_AB_SUFFIX_LEN; + } + } + for (; p < end; p++) { if (devNameLen != strlen(p->partition_name)) { continue; diff --git a/interfaces/innerkits/fs_manager/libfs_hvb/hvb_ops.c b/interfaces/innerkits/fs_manager/libfs_hvb/hvb_ops.c index 9673d8784..9aeec7d5f 100755 --- a/interfaces/innerkits/fs_manager/libfs_hvb/hvb_ops.c +++ b/interfaces/innerkits/fs_manager/libfs_hvb/hvb_ops.c @@ -23,6 +23,7 @@ #include "fs_dm.h" #include "fs_hvb.h" #include "securec.h" +#include "init_utils.h" #ifdef __cplusplus #if __cplusplus @@ -122,6 +123,57 @@ static char *GetExtHvbVerifiedPath(size_t index) return tmpPath; } +static char *HvbGetABPartitionPath(char *path, const size_t pathLen, const char *partition) +{ + /* Check if there are multiple partitions */ + int bootSlots = GetBootSlots(); + size_t ABpathLen = pathLen + FS_HVB_AB_SUFFIX_LEN; + if (bootSlots <= 1) { + BEGET_LOGE("invalid bootSlots: %d", bootSlots); + goto err; + } + + /* Confirm partition information */ + /* slot = 1 is partition a */ + /* slot = 2 is partition b */ + int slot = GetCurrentSlot(); + if (slot <= 0 || slot > MAX_SLOT) { + BEGET_LOGW("slot value %d is invalid, set default value", slot); + slot = 1; + } + + if (path != NULL) { + free(path); + } + path = calloc(1, ABpathLen + 1); + if (path == NULL) { + BEGET_LOGE("error, calloc fail"); + goto err; + } + + /* Concatenate partition names */ + int rc = snprintf_s(path, ABpathLen + 1, ABpathLen, "%s%s_%c", + PARTITION_PATH_PREFIX, partition, 'a' + slot - 1); + if (rc < 0) { + BEGET_LOGE("error, snprintf_s fail, ret = %d", rc); + goto err; + } + if (access(path, F_OK) != 0) { + BEGET_LOGE("still can not access %s, abort verify", path); + goto err; + } + + BEGET_LOGE("HvbGetABPartitionPath succ; Setting current slot to:%c", 'a' + slot - 1); + return path; + +err: + if (path != NULL) { + free(path); + } + + return NULL; +} + static char *HvbGetPartitionPath(const char *partition) { int rc; @@ -150,6 +202,11 @@ static char *HvbGetPartitionPath(const char *partition) free(path); return NULL; } + + if (access(path, F_OK) != 0) { + BEGET_LOGW("can not access %s, try to adapt into ab partition", path); + return HvbGetABPartitionPath(path, pathLen, partition); + } return path; } diff --git a/interfaces/innerkits/fs_manager/libfs_hvb/include/fs_hvb.h b/interfaces/innerkits/fs_manager/libfs_hvb/include/fs_hvb.h index 3aec80d21..363448e8f 100755 --- a/interfaces/innerkits/fs_manager/libfs_hvb/include/fs_hvb.h +++ b/interfaces/innerkits/fs_manager/libfs_hvb/include/fs_hvb.h @@ -46,7 +46,8 @@ extern "C" { #define SZ_2KB (2 * SZ_1KB) #define SZ_4KB (4 * SZ_1KB) #define FS_HVB_MAX_PATH_LEN 128 - +#define FS_HVB_AB_SUFFIX_LEN 2 + typedef struct { uint32_t magicNumber; uint16_t exthdrSize; diff --git a/test/unittest/BUILD.gn b/test/unittest/BUILD.gn index 300163d87..443fff272 100755 --- a/test/unittest/BUILD.gn +++ b/test/unittest/BUILD.gn @@ -503,6 +503,8 @@ ohos_unittest("init_dmverify_unittest") { ohos_unittest("init_fshvb_unittest") { module_out_path = "init/init" sources = [ + "//base/startup/init/interfaces/innerkits/fs_manager/fstab.c", + "//base/startup/init/interfaces/innerkits/fs_manager/fstab_mount.c", "//base/startup/init/interfaces/innerkits/fs_manager/libfs_hvb/fs_hvb.c", "//base/startup/init/interfaces/innerkits/fs_manager/libfs_hvb/hvb_ops.c", "//base/startup/init/services/log/init_commlog.c", @@ -521,6 +523,8 @@ ohos_unittest("init_fshvb_unittest") { "//base/startup/init/interfaces/innerkits/fs_manager/libfs_hvb/include", "//base/startup/hvb/libhvb/include", "//base/startup/init/interfaces/innerkits/fs_manager/libfs_dm/include", + "//base/startup/init/interfaces/innerkits/fs_manager/switch_root/include", + "//base/startup/init/interfaces/innerkits/init_module_engine/include", "//base/startup/init/interfaces/innerkits/include/fs_manager", "//base/startup/init/interfaces/innerkits/include", "//base/startup/init/services/log", @@ -534,6 +538,7 @@ ohos_unittest("init_fshvb_unittest") { configs = [] external_deps = [ + "cJSON:cjson", "c_utils:utils", "googletest:gtest", "hilog:libhilog", -- Gitee From cf36a0f169c5a58c7517f40cb7866e5e66a95e69 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=AB=98=E7=A5=A5?= Date: Wed, 23 Apr 2025 09:56:20 +0000 Subject: [PATCH 2/4] update interfaces/innerkits/fs_manager/libfs_hvb/hvb_ops.c. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 高祥 --- interfaces/innerkits/fs_manager/libfs_hvb/hvb_ops.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/interfaces/innerkits/fs_manager/libfs_hvb/hvb_ops.c b/interfaces/innerkits/fs_manager/libfs_hvb/hvb_ops.c index 9aeec7d5f..5b538b9a4 100755 --- a/interfaces/innerkits/fs_manager/libfs_hvb/hvb_ops.c +++ b/interfaces/innerkits/fs_manager/libfs_hvb/hvb_ops.c @@ -127,7 +127,7 @@ static char *HvbGetABPartitionPath(char *path, const size_t pathLen, const char { /* Check if there are multiple partitions */ int bootSlots = GetBootSlots(); - size_t ABpathLen = pathLen + FS_HVB_AB_SUFFIX_LEN; + size_t abPathLen = pathLen + FS_HVB_AB_SUFFIX_LEN; if (bootSlots <= 1) { BEGET_LOGE("invalid bootSlots: %d", bootSlots); goto err; @@ -145,14 +145,14 @@ static char *HvbGetABPartitionPath(char *path, const size_t pathLen, const char if (path != NULL) { free(path); } - path = calloc(1, ABpathLen + 1); + path = calloc(1, abPathLen + 1); if (path == NULL) { BEGET_LOGE("error, calloc fail"); goto err; } /* Concatenate partition names */ - int rc = snprintf_s(path, ABpathLen + 1, ABpathLen, "%s%s_%c", + int rc = snprintf_s(path, abPathLen + 1, abPathLen, "%s%s_%c", PARTITION_PATH_PREFIX, partition, 'a' + slot - 1); if (rc < 0) { BEGET_LOGE("error, snprintf_s fail, ret = %d", rc); -- Gitee From 1c47c307ecef16e94c134cf14d53e9e4013b7a14 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=AB=98=E7=A5=A5?= Date: Thu, 24 Apr 2025 04:00:28 +0000 Subject: [PATCH 3/4] update interfaces/innerkits/fs_manager/libfs_hvb/fs_hvb.c. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 高祥 --- interfaces/innerkits/fs_manager/libfs_hvb/fs_hvb.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/interfaces/innerkits/fs_manager/libfs_hvb/fs_hvb.c b/interfaces/innerkits/fs_manager/libfs_hvb/fs_hvb.c index 9e4a6f3fe..6fd4be4ba 100755 --- a/interfaces/innerkits/fs_manager/libfs_hvb/fs_hvb.c +++ b/interfaces/innerkits/fs_manager/libfs_hvb/fs_hvb.c @@ -321,7 +321,7 @@ static int FsHvbGetCert(struct hvb_cert *cert, const char *devName, struct hvb_v struct hvb_cert_data *p = vd->certs; struct hvb_cert_data *end = p + vd->num_loaded_certs; - int bootSlots = GetBootSlots(); + int bootSlots = GetBootSlots(); if (bootSlots > 1) { if (devNameLen <= FS_HVB_AB_SUFFIX_LEN) { BEGET_LOGE("error, devname (%s) is invlaid, devnamelen = %u", devName, devNameLen); -- Gitee From 59be44ec14e7c674159221e62c8aaff43d971183 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=AB=98=E7=A5=A5?= Date: Thu, 24 Apr 2025 04:02:12 +0000 Subject: [PATCH 4/4] update interfaces/innerkits/fs_manager/libfs_hvb/hvb_ops.c. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 高祥 --- interfaces/innerkits/fs_manager/libfs_hvb/hvb_ops.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/interfaces/innerkits/fs_manager/libfs_hvb/hvb_ops.c b/interfaces/innerkits/fs_manager/libfs_hvb/hvb_ops.c index 5b538b9a4..2c71765b5 100755 --- a/interfaces/innerkits/fs_manager/libfs_hvb/hvb_ops.c +++ b/interfaces/innerkits/fs_manager/libfs_hvb/hvb_ops.c @@ -153,7 +153,7 @@ static char *HvbGetABPartitionPath(char *path, const size_t pathLen, const char /* Concatenate partition names */ int rc = snprintf_s(path, abPathLen + 1, abPathLen, "%s%s_%c", - PARTITION_PATH_PREFIX, partition, 'a' + slot - 1); + PARTITION_PATH_PREFIX, partition, 'a' + slot - 1); if (rc < 0) { BEGET_LOGE("error, snprintf_s fail, ret = %d", rc); goto err; -- Gitee