diff --git a/interfaces/innerkits/fs_manager/libfs_hvb/fs_hvb.c b/interfaces/innerkits/fs_manager/libfs_hvb/fs_hvb.c index 4ec40a2070e5232c17217cd3c8b0a1425e169b16..6fd4be4ba174879998f0110fd587b701fbb67bd7 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 9673d8784b529b26050a3f295df68e3c83c43e7e..2c71765b5d7c44633ff6d753b4782a34491a8e86 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 3aec80d214d7b0f38346dba6017ef926af0cc4e1..363448e8fee2ed8514db558dbe03bde64f3a3818 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 300163d87e07cf950850202e4fea093e10b7de4c..443fff272665b8a4625ed52f606396def593adb3 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",