Skip to content

Commit

Permalink
[component/fs/romfs] support alignment for chained romfs
Browse files Browse the repository at this point in the history
  • Loading branch information
versaloon committed Aug 21, 2023
1 parent 9e240fb commit 5bb457b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
25 changes: 19 additions & 6 deletions source/component/fs/driver/romfs/vsf_romfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,23 @@ bool vsf_romfs_is_image_valid(vk_romfs_header_t *image)
return (image->nextfh == be32_to_cpu(0x2d726f6d)) && (image->spec == be32_to_cpu(0x3166732d));
}

vk_romfs_header_t * vsf_romfs_chain_get_next(vk_romfs_header_t *image)
vk_romfs_header_t * vsf_romfs_chain_get_next(vk_romfs_info_t *fsinfo, vk_romfs_header_t *image, bool force)
{
image = (vk_romfs_header_t *)((uint8_t *)image + be32_to_cpu(image->size));
return vsf_romfs_is_image_valid(image) ? image : NULL;
if (fsinfo->is_chained && vsf_romfs_is_image_valid(image)) {
uint8_t *ptr = (uint8_t *)image + be32_to_cpu(image->size);
uint32_t alignment = fsinfo->alignment;
if (alignment > 16) {
ptr = (uint8_t *)((uintptr_t)(ptr + alignment - 1) & ~(alignment - 1));
}

int diff = ptr - (uint8_t *)(fsinfo->image);
if ((diff < 0) || (diff >= fsinfo->image_size)) {
return NULL;
}
image = (vk_romfs_header_t *)ptr;
return (force || vsf_romfs_is_image_valid(image)) ? image : NULL;
}
return NULL;
}

static vk_romfs_header_t * __vsf_romfs_lookup_in_dir(vk_romfs_header_t *image, vk_romfs_header_t *dir, char *name)
Expand Down Expand Up @@ -163,7 +176,7 @@ static bool __vsf_romfs_should_hide(vk_romfs_header_t *image_head, vk_romfs_head
if ((header != NULL) && (__vsf_romfs_lookup_in_dir(image_head, header, name) != NULL)) {
return true;
}
image_head = (vk_romfs_header_t *)((uint8_t *)image_head + be32_to_cpu(image_head->size));
image_head = vsf_romfs_chain_get_next((vk_romfs_info_t *)dir->fsinfo, image_head, false);
}
return false;
}
Expand Down Expand Up @@ -269,8 +282,8 @@ __vsf_component_peda_ifs_entry(__vk_romfs_lookup, vk_file_lookup)
}
} else if (fsinfo->is_chained) {
while (true) {
image = vsf_romfs_chain_get_next(image);
if ((NULL == image) || ((uint8_t *)image >= ((uint8_t *)fsinfo->image + fsinfo->image_size))) {
image = vsf_romfs_chain_get_next(fsinfo, image, false);
if (NULL == image) {
goto not_found;
}

Expand Down
5 changes: 3 additions & 2 deletions source/component/fs/driver/romfs/vsf_romfs.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,9 @@ vsf_class(vk_romfs_info_t) {
vk_romfs_file_t root;

vk_romfs_header_t *image;
// image_size is necessary only if is_chained is true
// image_size/alignment is necessary only if is_chained is true
uint32_t image_size;
uint32_t alignment;
// used to chian multiple romfs
bool is_chained;
};
Expand All @@ -85,7 +86,7 @@ extern const vk_fs_op_t vk_romfs_op;

#if defined(__VSF_ROMFS_CLASS_INHERIT__) || defined(__VSF_ROMFS_CLASS_IMPLEMENT)
extern bool vsf_romfs_is_image_valid(vk_romfs_header_t *image);
extern vk_romfs_header_t * vsf_romfs_chain_get_next(vk_romfs_header_t *image);
extern vk_romfs_header_t * vsf_romfs_chain_get_next(vk_romfs_info_t *info, vk_romfs_header_t *image, bool force);
#endif

#ifdef __cplusplus
Expand Down

0 comments on commit 5bb457b

Please sign in to comment.