Skip to content

Commit

Permalink
resource: Support resource management
Browse files Browse the repository at this point in the history
- Support resource update based on bootparam
- Support resource binary header
- Verify resource binary size with partition size
- Add resource data to bootparam
  The "Resource Active Idx" is a index indicating which partition to use for resource.
   - All resource have 2 partitions.
   - If the value is 0, "First partition" will be mounted.
   - If the value is 1, "Second partition" will be mounted.
- Mount resource binary based on bootparam
- Add resource info to getinfo and getpath
- Support resource update with bootparam

Test result:
I put hello.txt in resource/ and verified it is read well.

TASH>>ls
/:
 dev/
 ext/
 mnt/
 proc/
 res/
 tmp/
 var/
TASH>>ls res
/res:
 .
 hello.txt

TASH>>cat res/hello.txt
hihihi
  • Loading branch information
jeongarmy authored and sunghan-chang committed Jun 20, 2024
1 parent 70bcadc commit a6655cf
Show file tree
Hide file tree
Showing 23 changed files with 688 additions and 73 deletions.
1 change: 1 addition & 0 deletions build/configs/common_download.sh
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ function get_executable_name()
loadparam) echo "$1";;
common) echo "${COMMON_BIN_NAME}";;
zoneinfo) echo "zoneinfo.img";;
resource) echo "${RESOURCE_BIN_NAME}";;
rom) echo "romfs.img";;
bootparam)
if [[ ! -n "${BOOTPARAM}" ]];then
Expand Down
6 changes: 6 additions & 0 deletions build/configs/rtl8730e/rtl8730e_download.sh
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ function pre_download()
if test -f "${BIN_PATH}/${COMMON_BIN_NAME}"; then
cp -p ${BIN_PATH}/${COMMON_BIN_NAME} ${IMG_TOOL_PATH}/${COMMON_BIN_NAME}
fi
if test -f "${BIN_PATH}/${RESOURCE_BIN_NAME}"; then
cp -p ${BIN_PATH}/${RESOURCE_BIN_NAME} ${IMG_TOOL_PATH}/${RESOURCE_BIN_NAME}
fi
if test -f "${SMARTFS_BIN_PATH}"; then
cp -p ${SMARTFS_BIN_PATH} ${IMG_TOOL_PATH}/${CONFIG_ARCH_BOARD}_smartfs.bin
fi
Expand Down Expand Up @@ -141,6 +144,9 @@ function post_download()
if test -f "${SMARTFS_BIN_PATH}"; then
[ -e ${CONFIG_ARCH_BOARD}_smartfs.bin ] && rm ${CONFIG_ARCH_BOARD}_smartfs.bin
fi
if test -f "${RESOURCE_BIN_PATH}"; then
[ -e ] && rm ${RESOURCE_BIN_PATH}
fi
if test -f "${BOOTPARAM}.bin"; then
[ -e ${BOOTPARAM}.bin ] && rm ${BOOTPARAM}.bin
fi
Expand Down
3 changes: 3 additions & 0 deletions framework/src/binary_manager/binary_manager_update.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ binmgr_result_type_e binary_manager_set_bootparam(uint8_t type, binary_setbp_res
#ifdef CONFIG_SUPPORT_COMMON_BINARY
&& !BM_CHECK_GROUP(type, BINARY_COMMON)
#endif
#endif
#ifdef CONFIG_RESOURCE_FS
&& !BM_CHECK_GROUP(type, BINARY_RESOURCE)
#endif
) {
bmdbg("Invalid parameter %u\n", type);
Expand Down
7 changes: 6 additions & 1 deletion os/Makefile.unix
Original file line number Diff line number Diff line change
Expand Up @@ -555,6 +555,11 @@ endif
ifeq ($(CONFIG_BINARY_SIGNING),y)
$(Q) $(call MAKE_USER_SIGNING)
endif
endif
ifeq ($(CONFIG_RESOURCE_FS),y)
$(Q) $(TOPDIR)/../tools/fs/mkromfsimg.sh $(TOPDIR)/../resource ../build/output/bin/resourcefs.img
$(Q) $(TOPDIR)/tools/mkbinheader.py $(OUTBIN_DIR)/resourcefs.img resource
$(Q) $(TOPDIR)/tools/mkchecksum.py $(OUTBIN_DIR)/resourcefs.img
endif
$(Q) echo "Start the Board Specific Work for Binary"
$(Q) $(call MAKE_BOARD_SPECIFIC_BIN, $(BIN_EXE), $(BIN_EXT))
Expand Down Expand Up @@ -789,7 +794,7 @@ endif

romfs:
ifeq ($(CONFIG_FS_ROMFS),y)
$(Q) ../tools/fs/mkromfsimg.sh
$(Q) $(TOPDIR)/../tools/fs/mkromfsimg.sh $(TOPDIR)/../tools/fs/contents-romfs ../build/output/bin/romfs.img
endif

# tools/compression/mkcompresstool.sh
Expand Down
144 changes: 109 additions & 35 deletions os/board/common/partitions.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,21 @@
#endif
#include "common.h"

#define FS_PATH_MAX 15
#define FS_PATH_MAX 16

#ifdef CONFIG_AUTOMOUNT_USERFS
#ifdef CONFIG_USERFS_MNTPT
#define USERFS_MNTPT CONFIG_USERFS_MNTPT
#else
#define USERFS_MNTPT "/mnt"
#endif

#ifdef CONFIG_USERFS_EXT_MNTPT
#define USERFS_EXT_MNTPT CONFIG_USERFS_EXT_MNTPT
#else
#define USERFS_EXT_MNTPT "/ext"
#endif
#endif /* CONFIG_AUTOMOUNT_USERFS */

#ifdef CONFIG_FLASH_PARTITION

Expand Down Expand Up @@ -155,7 +169,13 @@ static int type_specific_initialize(int minor, FAR struct mtd_dev_s *mtd_part, c
save_timezone_partno = true;
}
#endif
#endif /* CONFIG_MTD_FTL */
#ifdef CONFIG_RESOURCE_FS
else if (!strncmp(types, "resource,", 9)) {
do_ftlinit = true;
tagno = MTD_FTL;
}
#endif
#endif

#ifdef CONFIG_MTD_CONFIG
else if (!strncmp(types, "config,", 7)) {
Expand Down Expand Up @@ -251,6 +271,33 @@ static void configure_partition_name(FAR struct mtd_dev_s *mtd_part, const char
}
#endif

#ifdef CONFIG_RESOURCE_FS
static int make_resource_mtd_partition(struct mtd_dev_s *mtd, off_t partoffset, off_t nblocks, uint16_t partno)
{
int ret;
char fs_devname[FS_PATH_MAX];
FAR struct mtd_dev_s *mtd_part;
uint16_t resource_partno;

/* Set part num to avoid duplication */
resource_partno = partno + RESOURCE_DEVNUM_OFFSET;

mtd_part = mtd_partition(mtd, partoffset, nblocks, resource_partno);

if (!mtd_part) {
printf("ERROR: failed to create partition.\n");
return ERROR;
}

if (ftl_initialize(resource_partno, mtd_part)) {
printf("ERROR: failed to initialise mtd ftl errno :%d\n", errno);
return ERROR;
}

return OK;
}
#endif

int configure_mtd_partitions(struct mtd_dev_s *mtd, int minor, partition_info_t *partinfo)
{
int ret;
Expand All @@ -275,7 +322,7 @@ int configure_mtd_partitions(struct mtd_dev_s *mtd, int minor, partition_info_t
return ERROR;
#endif
}
g_partno = 0;

if (!mtd || !part_data.types || !part_data.sizes || !partinfo) {
printf("ERROR: Invalid partition data is NULL\n");
return ERROR;
Expand All @@ -292,6 +339,11 @@ int configure_mtd_partitions(struct mtd_dev_s *mtd, int minor, partition_info_t
return ERROR;
}

/* Initialize partinfo data */
partinfo->smartfs_partno = -1;
partinfo->romfs_partno = -1;
partinfo->timezone_partno = -1;

partoffset = 0;
types = part_data.types;
sizes = part_data.sizes;
Expand Down Expand Up @@ -335,21 +387,37 @@ int configure_mtd_partitions(struct mtd_dev_s *mtd, int minor, partition_info_t
#endif
}
#endif
#ifdef CONFIG_RESOURCE_FS
if (!strncmp(types, "resource,", 9)) {
int nblocks = geo.erasesize / geo.blocksize;
/* Make mtd dev to access resource fs. It starts from offset + erasesize (4K). */
ret = make_resource_mtd_partition(mtd, partoffset + (geo.erasesize / 1024), partsize / geo.blocksize - nblocks, g_partno);
if (ret != OK) {
printf("ERROR: fail to make resource mtd part.\n");
}
}
#endif
#ifdef CONFIG_MTD_PARTITION_NAMES
configure_partition_name(mtd_part, (const char **)&names, &index, part_name);
#if defined(CONFIG_BINARY_MANAGER) && defined(CONFIG_APP_BINARY_SEPARATION)
#ifdef CONFIG_BINARY_MANAGER
#ifdef CONFIG_APP_BINARY_SEPARATION
if (!strncmp(types, "bin,", 4)) {
binary_manager_register_upart(part_name, g_partno, partsize, partoffset * geo.blocksize);
}
#endif
#ifdef CONFIG_RESOURCE_FS
if (!strncmp(types, "resource,", 9)) {
binary_manager_register_respart(g_partno, partsize, partoffset * geo.blocksize);
}
#endif
#endif
#endif
partoffset += partsize / geo.blocksize;

move_to_next_part((const char **)&sizes);
move_to_next_part((const char **)&types);
g_partno++;
}

return OK;
}

Expand All @@ -365,47 +433,53 @@ void automount_fs_partition(partition_info_t *partinfo)
}
#ifdef CONFIG_AUTOMOUNT_USERFS
/* Initialize and mount user partition (if we have) */
snprintf(fs_devname, FS_PATH_MAX, "/dev/smart%dp%d", partinfo->minor, partinfo->smartfs_partno);
#ifdef CONFIG_SMARTFS_MULTI_ROOT_DIRS
ret = mksmartfs(fs_devname, 1, false);
#else
ret = mksmartfs(fs_devname, false);
#endif
if (ret != OK) {
printf("ERROR: mksmartfs on %s failed errno : %d\n", fs_devname, errno);
} else {
char *mountpath;
if (partinfo->minor == 0) {
mountpath = "/mnt";
} else {
mountpath = "/ext";
}
ret = mount(fs_devname, mountpath, "smartfs", 0, NULL);
if (partinfo->smartfs_partno != -1) {
snprintf(fs_devname, FS_PATH_MAX, "/dev/smart%dp%d", partinfo->minor, partinfo->smartfs_partno);
#ifdef CONFIG_SMARTFS_MULTI_ROOT_DIRS
ret = mksmartfs(fs_devname, 1, false);
#else
ret = mksmartfs(fs_devname, false);
#endif
if (ret != OK) {
printf("ERROR: mounting '%s' failed, errno %d\n", fs_devname, get_errno());
printf("ERROR: mksmartfs on %s failed errno : %d\n", fs_devname, errno);
} else {
printf("%s is mounted successfully @ %s \n", fs_devname, mountpath);
char *mountpath;
if (partinfo->minor == 0) {
mountpath = USERFS_MNTPT;
} else {
mountpath = USERFS_EXT_MNTPT;
}
ret = mount(fs_devname, mountpath, "smartfs", 0, NULL);
if (ret != OK) {
printf("ERROR: mounting '%s' failed, errno %d\n", fs_devname, get_errno());
} else {
printf("%s is mounted successfully @ %s \n", fs_devname, mountpath);
}
}
}
#endif

#ifdef CONFIG_AUTOMOUNT_ROMFS
snprintf(fs_devname, FS_PATH_MAX, "/dev/mtdblock%d", partinfo->romfs_partno);
ret = mount(fs_devname, "/rom", "romfs", 0, NULL);
if (ret != OK) {
printf("ERROR: mounting '%s'(ROMFS) failed, errno %d\n", fs_devname, get_errno());
} else {
printf("%s is mounted successfully @ %s \n", fs_devname, "/rom");
if (partinfo->romfs_partno != -1) {
snprintf(fs_devname, FS_PATH_MAX, "/dev/mtdblock%d", partinfo->romfs_partno);
ret = mount(fs_devname, "/rom", "romfs", 0, NULL);
if (ret != OK) {
printf("ERROR: mounting '%s'(ROMFS) failed, errno %d\n", fs_devname, get_errno());
} else {
printf("%s is mounted successfully @ %s \n", fs_devname, "/rom");
}
}
#endif /* CONFIG_AUTOMOUNT_ROMFS */

#ifdef CONFIG_LIBC_ZONEINFO_ROMFS
snprintf(fs_devname, FS_PATH_MAX, "/dev/mtdblock%d", partinfo->timezone_partno);
ret = mount(fs_devname, CONFIG_LIBC_TZDIR, "romfs", MS_RDONLY, NULL);
if (ret != OK) {
printf("ROMFS ERROR: timezone mount failed, errno %d\n", get_errno());
} else {
printf("%s is mounted successfully @ %s \n", fs_devname, CONFIG_LIBC_TZDIR);
if (partinfo->romfs_partno != -1) {
snprintf(fs_devname, FS_PATH_MAX, "/dev/mtdblock%d", partinfo->timezone_partno);
ret = mount(fs_devname, CONFIG_LIBC_TZDIR, "romfs", MS_RDONLY, NULL);
if (ret != OK) {
printf("ROMFS ERROR: timezone mount failed, errno %d\n", get_errno());
} else {
printf("%s is mounted successfully @ %s \n", fs_devname, CONFIG_LIBC_TZDIR);
}
}
#endif /* CONFIG_LIBC_ZONEINFO_ROMFS */
#endif /* CONFIG_AUTOMOUNT_USERFS, CONFIG_AUTOMOUNT_ROMFS, CONFIG_LIBC_ZONEINFO_ROMFS */
Expand Down
16 changes: 16 additions & 0 deletions os/fs/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,22 @@ config FS_WRITABLE
bool
default y

config RESOURCE_FS
bool "Support Resource fs"
select FS_ROMFS
default y

if RESOURCE_FS
config RESOURCE_BINARY_VERSION
string "Resource binary version (YYMMDD)"
default 240421
---help---
This config indicates resource binary version which has a "YYMMDD" format.
It is included in resource binary header and used to check the latest binary.
So it should be set to a higher value than versions of old binaries if you want to make the binary the latest version.

endif

source fs/aio/Kconfig
source fs/semaphore/Kconfig
source fs/mqueue/Kconfig
Expand Down
24 changes: 21 additions & 3 deletions os/include/tinyara/binary_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,21 +59,29 @@
#endif
#define KERNEL_BIN_COUNT 2

#define BINARY_COUNT (USER_BIN_COUNT + KERNEL_BIN_COUNT)
#define RESOURCE_DEVNUM_OFFSET 30
#define RESOURCE_HEADER_SIZE 4096
#define RESOURCE_BIN_COUNT 2

#ifdef CONFIG_RESOURCE_FS
#define BINARY_COUNT (USER_BIN_COUNT + KERNEL_BIN_COUNT + RESOURCE_BIN_COUNT)
#else
#define BINARY_COUNT (USER_BIN_COUNT + KERNEL_BIN_COUNT)
#endif
/* Kernel version has "YYMMDD" format */
#define KERNEL_BIN_VER_MIN 101 /* YYMMDD : 000101 */
#define KERNEL_BIN_VER_MAX 991231 /* YYMMDD : 991231 */

/* The lenght of user or kernel partition path */
#define BINARY_PATH_LEN 16

/* Binary Type : Kernel, Common, User app */
/* Binary Type : Kernel, Common, User app, Resource */
enum binary_type_e {
BINARY_KERNEL = 0,
BINARY_COMMON = 1,
BINARY_USERAPP = 2,
BINARY_TYPE_MAX = 3,
BINARY_RESOURCE = 3,
BINARY_TYPE_MAX = 4,
};

/* Macros for binary grouping used for request to set bootparam */
Expand Down Expand Up @@ -175,6 +183,14 @@ struct common_binary_header_s {
} __attribute__((__packed__));
typedef struct common_binary_header_s common_binary_header_t;

struct resource_binary_header_s {
uint32_t crc_hash;
uint16_t header_size;
uint32_t version;
uint32_t bin_size;
} __attribute__((__packed__));
typedef struct resource_binary_header_s resource_binary_header_t;

/* The structure of binary update information for kernel or user binaries */
struct binary_setbp_result_s {
int result[BINARY_TYPE_MAX];
Expand Down Expand Up @@ -280,6 +296,8 @@ typedef struct binmgr_getinfo_all_response_s binmgr_getinfo_all_response_t;
void binary_manager_register_kpart(int part_num, int part_size, int part_offset);
void binary_manager_register_bppart(int part_num, int part_size);
void binary_manager_register_upart(char *name, int part_num, int part_size, int part_offset);
void binary_manager_register_respart(int part_num, int part_size, int part_offset);
int binary_manager_mount_resource(void);
void binary_manager_deinit_modules(void);

#ifdef __cplusplus
Expand Down
4 changes: 4 additions & 0 deletions os/kernel/binary_manager/Make.defs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ ifeq ($(CONFIG_USE_BP),y)
CSRCS += binary_manager_bootparam.c
endif

ifeq ($(CONFIG_RESOURCE_FS),y)
CSRCS += binary_manager_resource.c
endif

ifeq ($(CONFIG_APP_BINARY_SEPARATION),y)
CSRCS += binary_manager_load.c binary_manager_callback.c binary_manager_deinit.c
ifeq ($(CONFIG_BINMGR_RECOVERY),y)
Expand Down
7 changes: 7 additions & 0 deletions os/kernel/binary_manager/binary_manager.c
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,13 @@ int binary_manager(int argc, char *argv[])
goto errout_with_nobinary;
}

#ifdef CONFIG_RESOURCE_FS
if (binary_manager_mount_resource() != OK) {
bmdbg("Fail to mount resource\n");
goto errout_with_nobinary;
}
#endif

#ifdef CONFIG_APP_BINARY_SEPARATION
if (binary_manager_get_ucount() <= 0) {
is_found_ubin = false;
Expand Down
Loading

0 comments on commit a6655cf

Please sign in to comment.