2008-09-10 21:27:49 +02:00
|
|
|
/****************************************************************************
|
2021-03-08 22:39:04 +01:00
|
|
|
* fs/romfs/fs_romfsutil.c
|
2008-09-10 21:27:49 +02:00
|
|
|
*
|
2020-08-09 19:40:35 +02:00
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership. The
|
|
|
|
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
2008-09-10 21:27:49 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-08-09 19:40:35 +02:00
|
|
|
/* References: Linux/Documentation/filesystems/romfs.txt */
|
|
|
|
|
2008-09-10 21:27:49 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
2020-11-22 02:53:11 +01:00
|
|
|
#include <inttypes.h>
|
2008-09-12 16:34:06 +02:00
|
|
|
#include <stdlib.h>
|
2008-09-10 21:27:49 +02:00
|
|
|
#include <string.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <errno.h>
|
2008-09-11 23:51:41 +02:00
|
|
|
#include <assert.h>
|
2008-09-10 21:27:49 +02:00
|
|
|
#include <debug.h>
|
|
|
|
|
2013-03-09 22:12:20 +01:00
|
|
|
#include <nuttx/kmalloc.h>
|
2012-03-21 19:01:07 +01:00
|
|
|
#include <nuttx/fs/ioctl.h>
|
2008-09-12 16:34:06 +02:00
|
|
|
|
2008-09-10 21:27:49 +02:00
|
|
|
#include "fs_romfs.h"
|
2024-09-09 14:28:16 +02:00
|
|
|
#include "fs_heap.h"
|
2008-09-10 21:27:49 +02:00
|
|
|
|
2020-08-09 19:40:35 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Pre-processor Definitions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#define LINK_NOT_FOLLOWED 0
|
|
|
|
#define LINK_FOLLOWED 1
|
2022-02-09 15:24:04 +01:00
|
|
|
#define NODEINFO_NINCR 4
|
2020-08-09 19:40:35 +02:00
|
|
|
|
2022-03-28 17:10:11 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Types
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
struct romfs_entryname_s
|
|
|
|
{
|
|
|
|
FAR const char *re_name;
|
|
|
|
size_t re_len;
|
|
|
|
};
|
|
|
|
|
2008-09-10 21:27:49 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: romfs_devread32
|
|
|
|
*
|
2018-02-01 19:03:55 +01:00
|
|
|
* Description:
|
2013-08-26 00:58:41 +02:00
|
|
|
* Read the big-endian 32-bit value from the mount device buffer
|
2008-09-10 21:27:49 +02:00
|
|
|
*
|
|
|
|
* Assumption:
|
|
|
|
* All values are aligned to 32-bit boundaries
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2022-02-10 04:41:50 +01:00
|
|
|
static uint32_t romfs_devread32(FAR struct romfs_mountpt_s *rm, int ndx)
|
2008-09-10 21:27:49 +02:00
|
|
|
{
|
2020-08-09 19:40:35 +02:00
|
|
|
/* This should not read past the end of the sector since the directory
|
|
|
|
* entries are aligned at 16-byte boundaries.
|
|
|
|
*/
|
|
|
|
|
2018-08-26 21:03:01 +02:00
|
|
|
return ((((uint32_t)rm->rm_buffer[ndx] & 0xff) << 24) |
|
|
|
|
(((uint32_t)rm->rm_buffer[ndx + 1] & 0xff) << 16) |
|
|
|
|
(((uint32_t)rm->rm_buffer[ndx + 2] & 0xff) << 8) |
|
|
|
|
((uint32_t)rm->rm_buffer[ndx + 3] & 0xff));
|
2008-09-10 21:27:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: romfs_checkentry
|
|
|
|
*
|
2018-02-01 19:03:55 +01:00
|
|
|
* Description:
|
2008-09-10 21:27:49 +02:00
|
|
|
* Check if the entry at offset is a directory or file path segment
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2022-02-09 15:24:04 +01:00
|
|
|
#ifndef CONFIG_FS_ROMFS_CACHE_NODE
|
2022-02-10 04:41:50 +01:00
|
|
|
static inline int romfs_checkentry(FAR struct romfs_mountpt_s *rm,
|
|
|
|
uint32_t offset,
|
|
|
|
FAR const char *entryname, int entrylen,
|
2022-02-21 09:27:01 +01:00
|
|
|
FAR struct romfs_nodeinfo_s *nodeinfo)
|
2008-09-10 21:27:49 +02:00
|
|
|
{
|
2019-03-01 22:00:00 +01:00
|
|
|
char name[NAME_MAX + 1];
|
2009-12-15 01:18:32 +01:00
|
|
|
uint32_t linkoffset;
|
|
|
|
uint32_t next;
|
|
|
|
uint32_t info;
|
|
|
|
uint32_t size;
|
2008-09-10 21:27:49 +02:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Parse the directory entry at this offset (which may be re-directed
|
|
|
|
* to some other entry if HARLINKED).
|
|
|
|
*/
|
|
|
|
|
|
|
|
ret = romfs_parsedirentry(rm, offset, &linkoffset, &next, &info, &size);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2022-03-10 14:43:51 +01:00
|
|
|
/* Get the name of the directory entry. */
|
|
|
|
|
|
|
|
ret = romfs_parsefilename(rm, offset, name);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Then check if this the name segment we are looking for. The
|
|
|
|
* string comparison is awkward because there is no terminator
|
|
|
|
* on entryname (there is a terminator on name, however)
|
2008-09-10 21:27:49 +02:00
|
|
|
*/
|
|
|
|
|
2022-03-10 14:43:51 +01:00
|
|
|
if (strlen(name) == entrylen &&
|
|
|
|
memcmp(entryname, name, entrylen) == 0)
|
2008-09-10 21:27:49 +02:00
|
|
|
{
|
2022-03-10 14:43:51 +01:00
|
|
|
/* Found it -- save the component info and return success */
|
2008-09-10 21:27:49 +02:00
|
|
|
|
2022-03-10 14:43:51 +01:00
|
|
|
if (IS_DIRECTORY(next))
|
2008-09-10 21:27:49 +02:00
|
|
|
{
|
2022-03-10 14:43:51 +01:00
|
|
|
nodeinfo->rn_offset = info;
|
|
|
|
nodeinfo->rn_size = 0;
|
2008-09-10 21:27:49 +02:00
|
|
|
}
|
2022-03-10 14:43:51 +01:00
|
|
|
else
|
2008-09-10 21:27:49 +02:00
|
|
|
{
|
2022-03-10 14:43:51 +01:00
|
|
|
nodeinfo->rn_offset = linkoffset;
|
|
|
|
nodeinfo->rn_size = size;
|
2008-09-10 21:27:49 +02:00
|
|
|
}
|
2022-03-10 14:43:51 +01:00
|
|
|
|
|
|
|
nodeinfo->rn_next = next;
|
|
|
|
return OK;
|
2008-09-10 21:27:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* The entry is not a directory or it does not have the matching name */
|
|
|
|
|
|
|
|
return -ENOENT;
|
|
|
|
}
|
2022-02-09 15:24:04 +01:00
|
|
|
#endif
|
2008-09-10 21:27:49 +02:00
|
|
|
|
2008-09-13 00:28:11 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: romfs_devcacheread
|
|
|
|
*
|
2018-02-01 19:03:55 +01:00
|
|
|
* Description:
|
2008-09-13 00:28:11 +02:00
|
|
|
* Read the specified sector for specified offset into the sector cache.
|
|
|
|
* Return the index into the sector corresponding to the offset
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2022-02-10 04:41:50 +01:00
|
|
|
static int16_t romfs_devcacheread(FAR struct romfs_mountpt_s *rm,
|
|
|
|
uint32_t offset)
|
2008-09-13 00:28:11 +02:00
|
|
|
{
|
2009-12-15 01:18:32 +01:00
|
|
|
uint32_t sector;
|
|
|
|
int ret;
|
2008-09-13 00:28:11 +02:00
|
|
|
|
2020-03-30 12:22:26 +02:00
|
|
|
/* rm->rm_cachesector holds the current sector that is buffer in or
|
2021-12-25 12:10:49 +01:00
|
|
|
* referenced by rm->rm_buffer. If the requested sector is the same as this
|
2020-03-30 12:22:26 +02:00
|
|
|
* this then we do nothing.
|
2008-09-13 00:28:11 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
sector = SEC_NSECTORS(rm, offset);
|
|
|
|
if (rm->rm_cachesector != sector)
|
|
|
|
{
|
|
|
|
/* Check the access mode */
|
|
|
|
|
|
|
|
if (rm->rm_xipbase)
|
|
|
|
{
|
|
|
|
/* In XIP mode, rf_buffer is just an offset pointer into the device
|
|
|
|
* address space.
|
|
|
|
*/
|
|
|
|
|
|
|
|
rm->rm_buffer = rm->rm_xipbase + SEC_ALIGN(rm, offset);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-10-11 19:39:29 +02:00
|
|
|
/* In non-XIP mode, we will have to read the new sector. */
|
2008-09-13 00:28:11 +02:00
|
|
|
|
|
|
|
ret = romfs_hwread(rm, rm->rm_buffer, sector, 1);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2019-10-27 18:48:14 +01:00
|
|
|
return (int16_t)ret;
|
2008-09-13 00:28:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Update the cached sector number */
|
|
|
|
|
|
|
|
rm->rm_cachesector = sector;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Return the offset */
|
|
|
|
|
|
|
|
return offset & SEC_NDXMASK(rm);
|
|
|
|
}
|
|
|
|
|
2008-09-12 22:54:14 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: romfs_followhardlinks
|
|
|
|
*
|
2018-02-01 19:03:55 +01:00
|
|
|
* Description:
|
2008-09-12 22:54:14 +02:00
|
|
|
* Given the offset to a file header, check if the file is a hardlink.
|
|
|
|
* If so, traverse the hard links until the terminal, non-linked header
|
|
|
|
* so found and return that offset.
|
|
|
|
*
|
2020-08-09 19:40:35 +02:00
|
|
|
* Return value:
|
|
|
|
* < 0 : An error occurred
|
|
|
|
* 0 : No link followed
|
|
|
|
* 1 : Link followed, poffset is the new volume offset
|
|
|
|
*
|
2008-09-12 22:54:14 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
2022-02-10 04:41:50 +01:00
|
|
|
static int romfs_followhardlinks(FAR struct romfs_mountpt_s *rm,
|
|
|
|
uint32_t offset, FAR uint32_t *poffset)
|
2008-09-12 22:54:14 +02:00
|
|
|
{
|
2009-12-15 01:18:32 +01:00
|
|
|
uint32_t next;
|
|
|
|
int16_t ndx;
|
|
|
|
int i;
|
2020-08-09 19:40:35 +02:00
|
|
|
int ret = LINK_NOT_FOLLOWED;
|
2008-09-12 22:54:14 +02:00
|
|
|
|
|
|
|
/* Loop while we are redirected by hardlinks */
|
|
|
|
|
|
|
|
for (i = 0; i < ROMF_MAX_LINKS; i++)
|
|
|
|
{
|
2008-09-13 00:28:11 +02:00
|
|
|
/* Read the sector containing the offset into memory */
|
2008-09-12 22:54:14 +02:00
|
|
|
|
2008-09-13 00:28:11 +02:00
|
|
|
ndx = romfs_devcacheread(rm, offset);
|
|
|
|
if (ndx < 0)
|
2008-09-12 22:54:14 +02:00
|
|
|
{
|
2008-09-13 00:28:11 +02:00
|
|
|
return ndx;
|
2008-09-12 22:54:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Check if this is a hard link */
|
|
|
|
|
|
|
|
next = romfs_devread32(rm, ndx + ROMFS_FHDR_NEXT);
|
|
|
|
if (!IS_HARDLINK(next))
|
|
|
|
{
|
|
|
|
*poffset = offset;
|
2020-08-09 19:40:35 +02:00
|
|
|
return ret;
|
2008-09-12 22:54:14 +02:00
|
|
|
}
|
|
|
|
|
2020-08-09 19:40:35 +02:00
|
|
|
/* Follow the hard-link. Set return to indicate that we followed a
|
|
|
|
* link and that poffset was set to the link offset is valid.
|
|
|
|
*/
|
2008-09-12 22:54:14 +02:00
|
|
|
|
|
|
|
offset = romfs_devread32(rm, ndx + ROMFS_FHDR_INFO);
|
2020-08-09 19:40:35 +02:00
|
|
|
ret = LINK_FOLLOWED;
|
2008-09-12 22:54:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return -ELOOP;
|
|
|
|
}
|
|
|
|
|
2022-02-09 15:24:04 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: romfs_nodeinfo_search/romfs_nodeinfo_compare
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Compare two names
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef CONFIG_FS_ROMFS_CACHE_NODE
|
|
|
|
static int romfs_nodeinfo_search(FAR const void *a, FAR const void *b)
|
|
|
|
{
|
|
|
|
FAR struct romfs_nodeinfo_s *nodeinfo = *(FAR struct romfs_nodeinfo_s **)b;
|
2022-03-28 17:10:11 +02:00
|
|
|
FAR const struct romfs_entryname_s *entry = a;
|
2022-02-09 15:24:04 +01:00
|
|
|
FAR const char *name2 = nodeinfo->rn_name;
|
|
|
|
size_t len = nodeinfo->rn_namesize;
|
|
|
|
int ret;
|
|
|
|
|
2022-03-28 17:10:11 +02:00
|
|
|
if (len > entry->re_len)
|
|
|
|
{
|
|
|
|
len = entry->re_len;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = strncmp(entry->re_name, name2, len);
|
2022-02-09 15:24:04 +01:00
|
|
|
if (!ret)
|
|
|
|
{
|
2022-03-28 17:10:11 +02:00
|
|
|
if (entry->re_name[len] == '/' || entry->re_name[len] == '\0')
|
2022-02-09 15:24:04 +01:00
|
|
|
{
|
2022-03-28 17:10:11 +02:00
|
|
|
return name2[len] == '\0' ? 0 : -1;
|
2022-02-09 15:24:04 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int romfs_nodeinfo_compare(FAR const void *a, FAR const void *b)
|
|
|
|
{
|
2022-03-28 17:10:11 +02:00
|
|
|
FAR struct romfs_nodeinfo_s *nodeinfo = *(FAR struct romfs_nodeinfo_s **)a;
|
|
|
|
struct romfs_entryname_s entry;
|
2022-02-09 15:24:04 +01:00
|
|
|
|
2022-03-28 17:10:11 +02:00
|
|
|
entry.re_name = nodeinfo->rn_name;
|
|
|
|
entry.re_len = nodeinfo->rn_namesize;
|
|
|
|
return romfs_nodeinfo_search(&entry, b);
|
2022-02-09 15:24:04 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2008-09-10 21:27:49 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: romfs_searchdir
|
|
|
|
*
|
2018-02-01 19:03:55 +01:00
|
|
|
* Description:
|
2008-09-10 21:27:49 +02:00
|
|
|
* This is part of the romfs_finddirentry log. Search the directory
|
2022-02-21 09:27:01 +01:00
|
|
|
* beginning at nodeinfo->rn_offset for entryname.
|
2008-09-10 21:27:49 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2022-02-10 04:41:50 +01:00
|
|
|
static inline int romfs_searchdir(FAR struct romfs_mountpt_s *rm,
|
|
|
|
FAR const char *entryname, int entrylen,
|
2022-02-21 09:27:01 +01:00
|
|
|
FAR struct romfs_nodeinfo_s *nodeinfo)
|
2008-09-10 21:27:49 +02:00
|
|
|
{
|
2022-02-09 15:24:04 +01:00
|
|
|
#ifdef CONFIG_FS_ROMFS_CACHE_NODE
|
|
|
|
FAR struct romfs_nodeinfo_s **cnodeinfo;
|
2022-03-28 17:10:11 +02:00
|
|
|
struct romfs_entryname_s entry;
|
2022-02-09 15:24:04 +01:00
|
|
|
|
2022-03-28 17:10:11 +02:00
|
|
|
entry.re_name = entryname;
|
|
|
|
entry.re_len = entrylen;
|
|
|
|
cnodeinfo = bsearch(&entry, nodeinfo->rn_child, nodeinfo->rn_count,
|
2022-03-27 21:46:49 +02:00
|
|
|
sizeof(*nodeinfo->rn_child), romfs_nodeinfo_search);
|
2022-02-09 15:24:04 +01:00
|
|
|
if (cnodeinfo)
|
|
|
|
{
|
|
|
|
memcpy(nodeinfo, *cnodeinfo, sizeof(*nodeinfo));
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
#else
|
2009-12-15 01:18:32 +01:00
|
|
|
uint32_t offset;
|
|
|
|
uint32_t next;
|
|
|
|
int16_t ndx;
|
|
|
|
int ret;
|
2008-09-10 21:27:49 +02:00
|
|
|
|
|
|
|
/* Then loop through the current directory until the directory
|
2008-09-11 23:51:41 +02:00
|
|
|
* with the matching name is found. Or until all of the entries
|
|
|
|
* the directory have been examined.
|
2008-09-10 21:27:49 +02:00
|
|
|
*/
|
|
|
|
|
2022-02-21 09:27:01 +01:00
|
|
|
offset = nodeinfo->rn_offset;
|
2008-09-11 23:51:41 +02:00
|
|
|
do
|
2008-09-10 21:27:49 +02:00
|
|
|
{
|
|
|
|
/* Read the sector into memory (do this before calling
|
|
|
|
* romfs_checkentry() so we won't have to read the sector
|
|
|
|
* twice in the event that the offset refers to a hardlink).
|
|
|
|
*/
|
|
|
|
|
2008-09-13 00:28:11 +02:00
|
|
|
ndx = romfs_devcacheread(rm, offset);
|
|
|
|
if (ndx < 0)
|
2008-09-10 21:27:49 +02:00
|
|
|
{
|
2008-09-13 00:28:11 +02:00
|
|
|
return ndx;
|
2008-09-10 21:27:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Because everything is chunked and aligned to 16-bit boundaries,
|
|
|
|
* we know that most the basic node info fits into the sector.
|
|
|
|
*/
|
|
|
|
|
|
|
|
next = romfs_devread32(rm, ndx + ROMFS_FHDR_NEXT) & RFNEXT_OFFSETMASK;
|
|
|
|
|
|
|
|
/* Check if the name this entry is a directory with the matching
|
|
|
|
* name
|
|
|
|
*/
|
|
|
|
|
2022-02-21 09:27:01 +01:00
|
|
|
ret = romfs_checkentry(rm, offset, entryname, entrylen, nodeinfo);
|
2008-09-10 21:27:49 +02:00
|
|
|
if (ret == OK)
|
|
|
|
{
|
2019-10-27 18:48:14 +01:00
|
|
|
/* Its a match! Return success */
|
2008-09-10 21:27:49 +02:00
|
|
|
|
2019-10-27 18:48:14 +01:00
|
|
|
return OK;
|
2008-09-10 21:27:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* No match... select the offset to the next entry */
|
|
|
|
|
2008-09-11 07:21:36 +02:00
|
|
|
offset = next;
|
2008-09-10 21:27:49 +02:00
|
|
|
}
|
2019-10-27 18:48:14 +01:00
|
|
|
while (next != 0);
|
2022-02-09 15:24:04 +01:00
|
|
|
#endif
|
2008-09-10 21:27:49 +02:00
|
|
|
|
2019-10-27 18:48:14 +01:00
|
|
|
/* There is nothing in this directory with that name */
|
2008-09-10 21:27:49 +02:00
|
|
|
|
2019-10-27 18:48:14 +01:00
|
|
|
return -ENOENT;
|
2008-09-10 21:27:49 +02:00
|
|
|
}
|
|
|
|
|
2022-02-09 15:24:04 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: romfs_cachenode
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Alloc all entry node at once when filesystem is mounted
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef CONFIG_FS_ROMFS_CACHE_NODE
|
|
|
|
static int romfs_cachenode(FAR struct romfs_mountpt_s *rm,
|
|
|
|
uint32_t offset, uint32_t next,
|
|
|
|
uint32_t size, FAR const char *name,
|
|
|
|
FAR struct romfs_nodeinfo_s **pnodeinfo)
|
|
|
|
{
|
|
|
|
FAR struct romfs_nodeinfo_s **child;
|
|
|
|
FAR struct romfs_nodeinfo_s *nodeinfo;
|
|
|
|
char childname[NAME_MAX + 1];
|
|
|
|
uint32_t linkoffset;
|
|
|
|
uint32_t info;
|
|
|
|
uint8_t num = 0;
|
2023-02-08 04:29:09 +01:00
|
|
|
size_t nsize;
|
2022-02-09 15:24:04 +01:00
|
|
|
int ret;
|
|
|
|
|
2023-02-08 04:29:09 +01:00
|
|
|
nsize = strlen(name);
|
2024-09-09 14:28:16 +02:00
|
|
|
nodeinfo = fs_heap_zalloc(sizeof(struct romfs_nodeinfo_s) + nsize);
|
2022-02-09 15:24:04 +01:00
|
|
|
if (nodeinfo == NULL)
|
|
|
|
{
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
*pnodeinfo = nodeinfo;
|
|
|
|
nodeinfo->rn_offset = offset;
|
|
|
|
nodeinfo->rn_next = next;
|
2023-02-08 04:29:09 +01:00
|
|
|
nodeinfo->rn_namesize = nsize;
|
|
|
|
strlcpy(nodeinfo->rn_name, name, nsize + 1);
|
2022-02-09 15:24:04 +01:00
|
|
|
if (!IS_DIRECTORY(next))
|
|
|
|
{
|
|
|
|
nodeinfo->rn_size = size;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
child = nodeinfo->rn_child;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
/* Parse the directory entry at this offset (which may be re-directed
|
|
|
|
* to some other entry if HARLINKED).
|
|
|
|
*/
|
|
|
|
|
|
|
|
ret = romfs_parsedirentry(rm, offset, &linkoffset, &next, &info,
|
|
|
|
&size);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2022-03-10 14:43:51 +01:00
|
|
|
ret = romfs_parsefilename(rm, offset, childname);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
2022-02-09 15:24:04 +01:00
|
|
|
|
2022-03-10 14:43:51 +01:00
|
|
|
if (strcmp(childname, ".") != 0 && strcmp(childname, "..") != 0)
|
2022-02-09 15:24:04 +01:00
|
|
|
{
|
2022-03-10 14:43:51 +01:00
|
|
|
if (child == NULL || nodeinfo->rn_count == num - 1)
|
2022-02-09 15:24:04 +01:00
|
|
|
{
|
2022-03-10 14:43:51 +01:00
|
|
|
FAR void *tmp;
|
2022-02-09 15:24:04 +01:00
|
|
|
|
2024-09-09 14:28:16 +02:00
|
|
|
tmp = fs_heap_realloc(nodeinfo->rn_child,
|
|
|
|
(num + NODEINFO_NINCR) * sizeof(*nodeinfo->rn_child));
|
2022-03-10 14:43:51 +01:00
|
|
|
if (tmp == NULL)
|
2022-02-09 15:24:04 +01:00
|
|
|
{
|
2022-03-10 14:43:51 +01:00
|
|
|
return -ENOMEM;
|
2022-02-09 15:24:04 +01:00
|
|
|
}
|
|
|
|
|
2022-03-10 14:43:51 +01:00
|
|
|
nodeinfo->rn_child = tmp;
|
|
|
|
memset(nodeinfo->rn_child + num, 0, NODEINFO_NINCR *
|
2022-03-27 21:46:49 +02:00
|
|
|
sizeof(*nodeinfo->rn_child));
|
2022-03-10 14:43:51 +01:00
|
|
|
num += NODEINFO_NINCR;
|
|
|
|
}
|
2022-02-09 15:24:04 +01:00
|
|
|
|
2022-03-10 14:43:51 +01:00
|
|
|
child = &nodeinfo->rn_child[nodeinfo->rn_count++];
|
|
|
|
if (IS_DIRECTORY(next))
|
|
|
|
{
|
|
|
|
linkoffset = info;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = romfs_cachenode(rm, linkoffset, next, size,
|
|
|
|
childname, child);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
nodeinfo->rn_count--;
|
|
|
|
return ret;
|
2022-02-09 15:24:04 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
next &= RFNEXT_OFFSETMASK;
|
|
|
|
offset = next;
|
|
|
|
}
|
|
|
|
while (next != 0);
|
|
|
|
|
|
|
|
if (nodeinfo->rn_count > 1)
|
|
|
|
{
|
|
|
|
qsort(nodeinfo->rn_child, nodeinfo->rn_count,
|
2022-03-27 21:46:49 +02:00
|
|
|
sizeof(*nodeinfo->rn_child), romfs_nodeinfo_compare);
|
2022-02-09 15:24:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2008-09-10 21:27:49 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: romfs_hwread
|
|
|
|
*
|
2018-02-01 19:03:55 +01:00
|
|
|
* Description: Read the specified sector into the sector buffer
|
2008-09-10 21:27:49 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2022-02-10 04:41:50 +01:00
|
|
|
int romfs_hwread(FAR struct romfs_mountpt_s *rm, FAR uint8_t *buffer,
|
2020-03-30 12:22:26 +02:00
|
|
|
uint32_t sector, unsigned int nsectors)
|
2008-09-10 21:27:49 +02:00
|
|
|
{
|
2019-01-27 19:07:37 +01:00
|
|
|
int ret = OK;
|
2008-09-10 21:27:49 +02:00
|
|
|
|
2008-09-12 16:34:06 +02:00
|
|
|
/* Check the access mode */
|
|
|
|
|
|
|
|
if (rm->rm_xipbase)
|
2008-09-10 21:27:49 +02:00
|
|
|
{
|
2008-09-12 16:34:06 +02:00
|
|
|
/* In XIP mode, we just copy the requested data */
|
|
|
|
|
|
|
|
memcpy(buffer,
|
2022-02-10 04:41:50 +01:00
|
|
|
rm->rm_xipbase + sector * rm->rm_hwsectorsize,
|
|
|
|
nsectors * rm->rm_hwsectorsize);
|
2008-09-12 16:34:06 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* In non-XIP mode, we have to read the data from the device */
|
|
|
|
|
2022-02-10 04:41:50 +01:00
|
|
|
FAR struct inode *inode = rm->rm_blkdriver;
|
2019-01-27 19:07:37 +01:00
|
|
|
ssize_t nsectorsread = -ENODEV;
|
2008-09-12 16:34:06 +02:00
|
|
|
|
2024-01-12 10:18:45 +01:00
|
|
|
nsectorsread =
|
|
|
|
inode->u.i_bops->read(inode, buffer, sector, nsectors);
|
2008-09-10 21:27:49 +02:00
|
|
|
|
2019-01-27 19:07:37 +01:00
|
|
|
if (nsectorsread == (ssize_t)nsectors)
|
|
|
|
{
|
|
|
|
ret = OK;
|
|
|
|
}
|
|
|
|
else if (nsectorsread < 0)
|
|
|
|
{
|
|
|
|
ret = nsectorsread;
|
2008-09-10 21:27:49 +02:00
|
|
|
}
|
|
|
|
}
|
2013-08-26 00:58:41 +02:00
|
|
|
|
2008-09-10 21:27:49 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: romfs_filecacheread
|
|
|
|
*
|
2018-02-01 19:03:55 +01:00
|
|
|
* Description:
|
2008-09-10 21:27:49 +02:00
|
|
|
* Read the specified sector into the sector cache
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2022-02-10 04:41:50 +01:00
|
|
|
int romfs_filecacheread(FAR struct romfs_mountpt_s *rm,
|
|
|
|
FAR struct romfs_file_s *rf, uint32_t sector)
|
2008-09-10 21:27:49 +02:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
2022-01-04 11:33:11 +01:00
|
|
|
finfo("sector: %" PRId32 " cached: %" PRId32 " ncached: %" PRId32 ""
|
2020-11-22 02:53:11 +01:00
|
|
|
" sectorsize: %d XIP base: %p buffer: %p\n",
|
2022-01-04 11:33:11 +01:00
|
|
|
sector, rf->rf_cachesector, rf->rf_ncachesector,
|
|
|
|
rm->rm_hwsectorsize, rm->rm_xipbase, rf->rf_buffer);
|
2009-06-25 01:33:33 +02:00
|
|
|
|
2020-03-30 12:22:26 +02:00
|
|
|
/* rf->rf_cachesector holds the current sector that is buffer in or
|
|
|
|
* referenced by rf->rf_buffer. If the requested sector is the same as this
|
|
|
|
* sector then we do nothing.
|
2008-09-10 21:27:49 +02:00
|
|
|
*/
|
|
|
|
|
2022-01-04 11:33:11 +01:00
|
|
|
if (rf->rf_cachesector > sector ||
|
|
|
|
rf->rf_cachesector + rf->rf_ncachesector <= sector)
|
2008-09-12 16:34:06 +02:00
|
|
|
{
|
|
|
|
/* Check the access mode */
|
|
|
|
|
|
|
|
if (rm->rm_xipbase)
|
|
|
|
{
|
|
|
|
/* In XIP mode, rf_buffer is just an offset pointer into the device
|
|
|
|
* address space.
|
|
|
|
*/
|
|
|
|
|
2008-09-13 00:28:11 +02:00
|
|
|
rf->rf_buffer = rm->rm_xipbase + sector * rm->rm_hwsectorsize;
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("XIP buffer: %p\n", rf->rf_buffer);
|
2008-09-12 16:34:06 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-01-04 11:33:11 +01:00
|
|
|
if (sector + rf->rf_ncachesector - 1 > rf->rf_endsector)
|
|
|
|
{
|
|
|
|
sector = rf->rf_endsector + 1 - rf->rf_ncachesector;
|
|
|
|
}
|
|
|
|
|
2015-10-11 19:39:29 +02:00
|
|
|
/* In non-XIP mode, we will have to read the new sector. */
|
2008-09-12 16:34:06 +02:00
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("Calling romfs_hwread\n");
|
2022-01-04 11:33:11 +01:00
|
|
|
ret = romfs_hwread(rm, rf->rf_buffer, sector, rf->rf_ncachesector);
|
2008-09-12 16:34:06 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-12 01:14:02 +02:00
|
|
|
ferr("ERROR: romfs_hwread failed: %d\n", ret);
|
2008-09-12 16:34:06 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
2008-09-10 21:27:49 +02:00
|
|
|
|
2008-09-12 16:34:06 +02:00
|
|
|
/* Update the cached sector number */
|
2008-09-10 21:27:49 +02:00
|
|
|
|
2008-09-12 16:34:06 +02:00
|
|
|
rf->rf_cachesector = sector;
|
2008-09-10 21:27:49 +02:00
|
|
|
}
|
2008-09-13 00:28:11 +02:00
|
|
|
|
2008-09-12 16:34:06 +02:00
|
|
|
return OK;
|
2008-09-10 21:27:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2008-09-12 16:34:06 +02:00
|
|
|
* Name: romfs_hwconfigure
|
2008-09-10 21:27:49 +02:00
|
|
|
*
|
2018-02-01 19:03:55 +01:00
|
|
|
* Description:
|
2020-03-30 12:22:26 +02:00
|
|
|
* This function is called as part of the ROMFS mount operation.
|
|
|
|
* It configures the ROMFS filestem for use on this block driver. This
|
|
|
|
* include the accounting for the geometry of the device, setting up any
|
|
|
|
* XIP modes of operation, and/or allocating any cache buffers.
|
2008-09-10 21:27:49 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2022-02-10 04:41:50 +01:00
|
|
|
int romfs_hwconfigure(FAR struct romfs_mountpt_s *rm)
|
2008-09-10 21:27:49 +02:00
|
|
|
{
|
2022-02-10 04:41:50 +01:00
|
|
|
FAR struct inode *inode = rm->rm_blkdriver;
|
2024-01-12 10:18:45 +01:00
|
|
|
struct geometry geo;
|
2008-09-10 21:27:49 +02:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Get the underlying device geometry */
|
|
|
|
|
2016-06-11 22:14:08 +02:00
|
|
|
#ifdef CONFIG_DEBUG_FEATURES
|
2019-01-27 19:07:37 +01:00
|
|
|
if (inode == NULL)
|
2008-09-10 21:27:49 +02:00
|
|
|
{
|
|
|
|
return -ENODEV;
|
|
|
|
}
|
2008-09-12 16:34:06 +02:00
|
|
|
#endif
|
2008-09-10 21:27:49 +02:00
|
|
|
|
2024-01-12 10:18:45 +01:00
|
|
|
ret = inode->u.i_bops->geometry(inode, &geo);
|
|
|
|
if (ret != OK)
|
2008-09-10 21:27:49 +02:00
|
|
|
{
|
2024-01-12 10:18:45 +01:00
|
|
|
return ret;
|
2008-09-10 21:27:49 +02:00
|
|
|
}
|
2019-01-27 19:07:37 +01:00
|
|
|
|
2024-01-12 10:18:45 +01:00
|
|
|
if (!geo.geo_available)
|
|
|
|
{
|
|
|
|
return -EBUSY;
|
|
|
|
}
|
2019-01-27 19:07:37 +01:00
|
|
|
|
2024-01-12 10:18:45 +01:00
|
|
|
/* Save that information in the mount structure */
|
2008-09-10 21:27:49 +02:00
|
|
|
|
2024-01-12 10:18:45 +01:00
|
|
|
rm->rm_hwsectorsize = geo.geo_sectorsize;
|
|
|
|
rm->rm_hwnsectors = geo.geo_nsectors;
|
2008-09-12 16:34:06 +02:00
|
|
|
|
|
|
|
/* Determine if block driver supports the XIP mode of operation */
|
|
|
|
|
2022-02-21 09:27:01 +01:00
|
|
|
rm->rm_cachesector = (uint32_t)-1;
|
2008-09-12 16:34:06 +02:00
|
|
|
|
2024-01-12 10:18:45 +01:00
|
|
|
if (inode->u.i_bops->ioctl)
|
2008-09-12 16:34:06 +02:00
|
|
|
{
|
|
|
|
ret = inode->u.i_bops->ioctl(inode, BIOC_XIPBASE,
|
|
|
|
(unsigned long)&rm->rm_xipbase);
|
2024-01-12 10:18:45 +01:00
|
|
|
if (ret == OK && rm->rm_xipbase)
|
|
|
|
{
|
|
|
|
/* Yes.. Then we will directly access the media (vs.
|
|
|
|
* copying into an allocated sector buffer.
|
|
|
|
*/
|
2019-01-27 19:07:37 +01:00
|
|
|
|
2024-01-12 10:18:45 +01:00
|
|
|
rm->rm_buffer = rm->rm_xipbase;
|
|
|
|
rm->rm_cachesector = 0;
|
|
|
|
return OK;
|
|
|
|
}
|
2008-09-12 16:34:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Allocate the device cache buffer for normal sector accesses */
|
|
|
|
|
2024-09-09 14:28:16 +02:00
|
|
|
rm->rm_buffer = fs_heap_malloc(rm->rm_hwsectorsize);
|
2008-09-12 16:34:06 +02:00
|
|
|
if (!rm->rm_buffer)
|
|
|
|
{
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
2008-09-10 21:27:49 +02:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2008-09-12 16:34:06 +02:00
|
|
|
* Name: romfs_fsconfigure
|
2008-09-10 21:27:49 +02:00
|
|
|
*
|
2018-02-01 19:03:55 +01:00
|
|
|
* Description:
|
2008-09-12 16:34:06 +02:00
|
|
|
* This function is called as part of the ROMFS mount operation It
|
2020-03-30 12:22:26 +02:00
|
|
|
* sets up the mount structure to include configuration information
|
|
|
|
* contained in the ROMFS header. This is the place where we actually
|
|
|
|
* determine if the media contains a ROMFS filesystem.
|
2008-09-10 21:27:49 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2022-02-10 04:41:50 +01:00
|
|
|
int romfs_fsconfigure(FAR struct romfs_mountpt_s *rm)
|
2008-09-10 21:27:49 +02:00
|
|
|
{
|
2022-02-10 04:41:50 +01:00
|
|
|
FAR const char *name;
|
|
|
|
int16_t ndx;
|
2008-09-10 21:27:49 +02:00
|
|
|
|
|
|
|
/* Then get information about the ROMFS filesystem on the devices managed
|
2020-03-30 12:22:26 +02:00
|
|
|
* by this block driver. Read sector zero which contains the volume header.
|
2008-09-10 21:27:49 +02:00
|
|
|
*/
|
|
|
|
|
2008-09-13 00:28:11 +02:00
|
|
|
ndx = romfs_devcacheread(rm, 0);
|
|
|
|
if (ndx < 0)
|
2008-09-10 21:27:49 +02:00
|
|
|
{
|
2008-09-13 00:28:11 +02:00
|
|
|
return ndx;
|
2008-09-10 21:27:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Verify the magic number at that identifies this as a ROMFS filesystem */
|
|
|
|
|
|
|
|
if (memcmp(rm->rm_buffer, ROMFS_VHDR_MAGIC, 8) != 0)
|
|
|
|
{
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Then extract the values we need from the header and return success */
|
|
|
|
|
|
|
|
rm->rm_volsize = romfs_devread32(rm, ROMFS_VHDR_SIZE);
|
|
|
|
|
|
|
|
/* The root directory entry begins right after the header */
|
|
|
|
|
2015-10-11 19:39:29 +02:00
|
|
|
name = (FAR const char *)&rm->rm_buffer[ROMFS_VHDR_VOLNAME];
|
2022-02-09 15:24:04 +01:00
|
|
|
#ifdef CONFIG_FS_ROMFS_CACHE_NODE
|
|
|
|
ndx = romfs_cachenode(rm, ROMFS_ALIGNUP(ROMFS_VHDR_VOLNAME +
|
|
|
|
strlen(name) + 1),
|
|
|
|
RFNEXT_DIRECTORY, 0, "", &rm->rm_root);
|
|
|
|
if (ndx < 0)
|
|
|
|
{
|
|
|
|
romfs_freenode(rm->rm_root);
|
|
|
|
return ndx;
|
|
|
|
}
|
|
|
|
#else
|
2008-09-10 21:27:49 +02:00
|
|
|
rm->rm_rootoffset = ROMFS_ALIGNUP(ROMFS_VHDR_VOLNAME + strlen(name) + 1);
|
2022-02-09 15:24:04 +01:00
|
|
|
#endif
|
2008-09-10 21:27:49 +02:00
|
|
|
|
|
|
|
/* and return success */
|
|
|
|
|
2013-08-26 00:58:41 +02:00
|
|
|
rm->rm_mounted = true;
|
2008-09-10 21:27:49 +02:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2008-09-12 16:34:06 +02:00
|
|
|
/****************************************************************************
|
2013-01-14 16:42:03 +01:00
|
|
|
* Name: romfs_fileconfigure
|
2008-09-12 16:34:06 +02:00
|
|
|
*
|
2018-02-01 19:03:55 +01:00
|
|
|
* Description:
|
2008-09-12 16:34:06 +02:00
|
|
|
* This function is called as part of the ROMFS file open operation It
|
|
|
|
* sets up the file structure to handle buffer appropriately, depending
|
|
|
|
* upon XIP mode or not.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2022-02-10 04:41:50 +01:00
|
|
|
int romfs_fileconfigure(FAR struct romfs_mountpt_s *rm,
|
|
|
|
FAR struct romfs_file_s *rf)
|
2008-09-12 16:34:06 +02:00
|
|
|
{
|
|
|
|
/* Check if XIP access mode is supported. If so, then we do not need
|
|
|
|
* to allocate anything.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (rm->rm_xipbase)
|
|
|
|
{
|
|
|
|
/* We'll put a valid address in rf_buffer just in case. */
|
|
|
|
|
2022-01-04 11:33:11 +01:00
|
|
|
rf->rf_cachesector = 0;
|
|
|
|
rf->rf_buffer = rm->rm_xipbase;
|
|
|
|
rf->rf_ncachesector = 1;
|
2008-09-12 16:34:06 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-01-04 11:33:11 +01:00
|
|
|
uint32_t nsectors;
|
|
|
|
|
|
|
|
rf->rf_endsector = SEC_NSECTORS(rm, rf->rf_startoffset + rf->rf_size);
|
|
|
|
nsectors = rf->rf_endsector - SEC_NSECTORS(rm, rf->rf_startoffset) + 1;
|
|
|
|
if (nsectors > CONFIG_FS_ROMFS_CACHE_FILE_NSECTORS)
|
|
|
|
{
|
|
|
|
nsectors = CONFIG_FS_ROMFS_CACHE_FILE_NSECTORS;
|
|
|
|
}
|
|
|
|
|
2008-09-12 16:34:06 +02:00
|
|
|
/* Nothing in the cache buffer */
|
|
|
|
|
2009-12-15 01:18:32 +01:00
|
|
|
rf->rf_cachesector = (uint32_t)-1;
|
2022-01-04 11:33:11 +01:00
|
|
|
rf->rf_ncachesector = nsectors;
|
2008-09-12 16:34:06 +02:00
|
|
|
|
|
|
|
/* Create a file buffer to support partial sector accesses */
|
|
|
|
|
2024-09-09 14:28:16 +02:00
|
|
|
rf->rf_buffer = fs_heap_malloc(rm->rm_hwsectorsize *
|
|
|
|
rf->rf_ncachesector);
|
2008-09-12 16:34:06 +02:00
|
|
|
if (!rf->rf_buffer)
|
|
|
|
{
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
}
|
2013-01-14 16:42:03 +01:00
|
|
|
|
2008-09-12 16:34:06 +02:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2008-09-10 21:27:49 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: romfs_checkmount
|
|
|
|
*
|
2018-02-01 19:03:55 +01:00
|
|
|
* Description: Check if the mountpoint is still valid.
|
2008-09-10 21:27:49 +02:00
|
|
|
*
|
|
|
|
* The caller should hold the mountpoint semaphore
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2022-02-10 04:41:50 +01:00
|
|
|
int romfs_checkmount(FAR struct romfs_mountpt_s *rm)
|
2008-09-10 21:27:49 +02:00
|
|
|
{
|
2022-02-10 04:41:50 +01:00
|
|
|
FAR struct inode *inode;
|
2008-09-12 16:34:06 +02:00
|
|
|
struct geometry geo;
|
|
|
|
int ret;
|
|
|
|
|
2009-12-15 01:18:32 +01:00
|
|
|
/* If the fs_mounted flag is false, then we have already handled the loss
|
2008-09-10 21:27:49 +02:00
|
|
|
* of the mount.
|
|
|
|
*/
|
|
|
|
|
2008-09-12 16:34:06 +02:00
|
|
|
DEBUGASSERT(rm && rm->rm_blkdriver);
|
|
|
|
if (rm->rm_mounted)
|
2008-09-10 21:27:49 +02:00
|
|
|
{
|
|
|
|
/* We still think the mount is healthy. Check an see if this is
|
|
|
|
* still the case
|
|
|
|
*/
|
|
|
|
|
2008-09-12 16:34:06 +02:00
|
|
|
inode = rm->rm_blkdriver;
|
2024-01-12 10:18:45 +01:00
|
|
|
if (inode->u.i_bops->geometry)
|
2008-09-10 21:27:49 +02:00
|
|
|
{
|
2008-09-12 16:34:06 +02:00
|
|
|
ret = inode->u.i_bops->geometry(inode, &geo);
|
|
|
|
if (ret == OK && geo.geo_available && !geo.geo_mediachanged)
|
2008-09-10 21:27:49 +02:00
|
|
|
{
|
2008-09-12 16:34:06 +02:00
|
|
|
return OK;
|
2008-09-10 21:27:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If we get here, the mount is NOT healthy */
|
|
|
|
|
2009-12-15 01:18:32 +01:00
|
|
|
rm->rm_mounted = false;
|
2008-09-10 21:27:49 +02:00
|
|
|
}
|
2013-01-14 16:42:03 +01:00
|
|
|
|
2008-09-10 21:27:49 +02:00
|
|
|
return -ENODEV;
|
|
|
|
}
|
|
|
|
|
2022-02-09 15:24:04 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: romfs_freenode
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* free all entry node at once when filesystem is unmounted
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef CONFIG_FS_ROMFS_CACHE_NODE
|
|
|
|
void romfs_freenode(FAR struct romfs_nodeinfo_s *nodeinfo)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (IS_DIRECTORY(nodeinfo->rn_next))
|
|
|
|
{
|
|
|
|
for (i = 0; i < nodeinfo->rn_count; i++)
|
|
|
|
{
|
|
|
|
romfs_freenode(nodeinfo->rn_child[i]);
|
|
|
|
}
|
|
|
|
|
2024-09-09 14:28:16 +02:00
|
|
|
fs_heap_free(nodeinfo->rn_child);
|
2022-02-09 15:24:04 +01:00
|
|
|
}
|
|
|
|
|
2024-09-09 14:28:16 +02:00
|
|
|
fs_heap_free(nodeinfo);
|
2022-02-09 15:24:04 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2008-09-10 21:27:49 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: romfs_finddirentry
|
|
|
|
*
|
2018-02-01 19:03:55 +01:00
|
|
|
* Description:
|
2008-09-10 21:27:49 +02:00
|
|
|
* Given a path to something that may or may not be in the file system,
|
|
|
|
* return the directory entry of the item.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2022-02-10 04:41:50 +01:00
|
|
|
int romfs_finddirentry(FAR struct romfs_mountpt_s *rm,
|
2022-02-21 09:27:01 +01:00
|
|
|
FAR struct romfs_nodeinfo_s *nodeinfo,
|
2022-02-10 04:41:50 +01:00
|
|
|
FAR const char *path)
|
2008-09-10 21:27:49 +02:00
|
|
|
{
|
2022-02-10 04:41:50 +01:00
|
|
|
FAR const char *entryname;
|
|
|
|
FAR const char *terminator;
|
2008-09-10 21:27:49 +02:00
|
|
|
int entrylen;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Start with the first element after the root directory */
|
|
|
|
|
2022-02-09 15:24:04 +01:00
|
|
|
#ifdef CONFIG_FS_ROMFS_CACHE_NODE
|
|
|
|
memcpy(nodeinfo, rm->rm_root, sizeof(*nodeinfo));
|
|
|
|
#else
|
2022-02-21 09:27:01 +01:00
|
|
|
nodeinfo->rn_offset = rm->rm_rootoffset;
|
|
|
|
nodeinfo->rn_next = RFNEXT_DIRECTORY;
|
|
|
|
nodeinfo->rn_size = 0;
|
2022-02-09 15:24:04 +01:00
|
|
|
#endif
|
2008-09-10 21:27:49 +02:00
|
|
|
|
2008-09-11 07:21:36 +02:00
|
|
|
/* The root directory is a special case */
|
|
|
|
|
|
|
|
if (!path || path[0] == '\0')
|
|
|
|
{
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2008-09-10 21:27:49 +02:00
|
|
|
/* Then loop for each directory/file component in the full path */
|
|
|
|
|
2022-02-10 04:41:50 +01:00
|
|
|
entryname = path;
|
2008-09-10 21:27:49 +02:00
|
|
|
terminator = NULL;
|
|
|
|
|
2015-10-11 19:39:29 +02:00
|
|
|
for (; ; )
|
2008-09-10 21:27:49 +02:00
|
|
|
{
|
|
|
|
/* Find the start of the next path component */
|
|
|
|
|
|
|
|
while (*entryname == '/') entryname++;
|
|
|
|
|
|
|
|
/* Find the end of the next path component */
|
|
|
|
|
|
|
|
terminator = strchr(entryname, '/');
|
|
|
|
if (!terminator)
|
|
|
|
{
|
|
|
|
entrylen = strlen(entryname);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
entrylen = terminator - entryname;
|
|
|
|
}
|
|
|
|
|
2020-07-30 20:51:05 +02:00
|
|
|
if (entrylen == 0)
|
|
|
|
{
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2008-09-10 21:27:49 +02:00
|
|
|
/* Long path segment names will be truncated to NAME_MAX */
|
|
|
|
|
|
|
|
if (entrylen > NAME_MAX)
|
|
|
|
{
|
|
|
|
entrylen = NAME_MAX;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Then find the entry in the current directory with the
|
|
|
|
* matching name.
|
|
|
|
*/
|
|
|
|
|
2022-02-21 09:27:01 +01:00
|
|
|
ret = romfs_searchdir(rm, entryname, entrylen, nodeinfo);
|
2008-09-10 21:27:49 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Was that the last path component? */
|
|
|
|
|
|
|
|
if (!terminator)
|
|
|
|
{
|
2019-10-27 18:48:14 +01:00
|
|
|
/* Yes.. return success */
|
2008-09-10 21:27:49 +02:00
|
|
|
|
2019-10-27 18:48:14 +01:00
|
|
|
return OK;
|
2008-09-10 21:27:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* No... If that was not the last path component, then it had
|
|
|
|
* better have been a directory
|
|
|
|
*/
|
|
|
|
|
2022-02-21 09:27:01 +01:00
|
|
|
if (!IS_DIRECTORY(nodeinfo->rn_next))
|
2008-09-10 21:27:49 +02:00
|
|
|
{
|
|
|
|
return -ENOTDIR;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Setup to search the next directory for the next component
|
|
|
|
* of the path
|
|
|
|
*/
|
|
|
|
|
|
|
|
entryname = terminator;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ERROR; /* Won't get here */
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: romfs_parsedirentry
|
|
|
|
*
|
2018-02-01 19:03:55 +01:00
|
|
|
* Description:
|
2008-09-10 21:27:49 +02:00
|
|
|
* Return the directory entry at this offset. If rf is NULL, then the
|
|
|
|
* mount device resources are used. Otherwise, file resources are used.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2022-02-10 04:41:50 +01:00
|
|
|
int romfs_parsedirentry(FAR struct romfs_mountpt_s *rm, uint32_t offset,
|
|
|
|
FAR uint32_t *poffset, uint32_t *pnext,
|
|
|
|
FAR uint32_t *pinfo, FAR uint32_t *psize)
|
2008-09-10 21:27:49 +02:00
|
|
|
{
|
2009-12-15 01:18:32 +01:00
|
|
|
uint32_t save;
|
|
|
|
uint32_t next;
|
|
|
|
int16_t ndx;
|
|
|
|
int ret;
|
2008-09-10 21:27:49 +02:00
|
|
|
|
2008-09-12 22:54:14 +02:00
|
|
|
/* Read the sector into memory */
|
2008-09-10 21:27:49 +02:00
|
|
|
|
2008-09-13 00:28:11 +02:00
|
|
|
ndx = romfs_devcacheread(rm, offset);
|
|
|
|
if (ndx < 0)
|
2008-09-12 22:54:14 +02:00
|
|
|
{
|
2008-09-13 00:28:11 +02:00
|
|
|
return ndx;
|
2008-09-12 22:54:14 +02:00
|
|
|
}
|
2008-09-10 21:27:49 +02:00
|
|
|
|
2008-09-12 22:54:14 +02:00
|
|
|
/* Yes.. Save the first 'next' value. That has the offset needed to
|
|
|
|
* traverse the parent directory. But we may need to change the type
|
|
|
|
* after we follow the hard links.
|
|
|
|
*/
|
2008-09-10 21:27:49 +02:00
|
|
|
|
2008-09-12 22:54:14 +02:00
|
|
|
save = romfs_devread32(rm, ndx + ROMFS_FHDR_NEXT);
|
2008-09-10 21:27:49 +02:00
|
|
|
|
2019-09-20 02:19:18 +02:00
|
|
|
/* Traverse hardlinks as necessary to get to the real file header */
|
2008-09-10 21:27:49 +02:00
|
|
|
|
2008-09-12 22:54:14 +02:00
|
|
|
ret = romfs_followhardlinks(rm, offset, poffset);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
2008-09-10 21:27:49 +02:00
|
|
|
}
|
2020-08-09 19:40:35 +02:00
|
|
|
else if (ret > 0)
|
|
|
|
{
|
|
|
|
/* The link was followed */
|
|
|
|
|
|
|
|
ndx = romfs_devcacheread(rm, *poffset);
|
|
|
|
if (ndx < 0)
|
|
|
|
{
|
|
|
|
return ndx;
|
|
|
|
}
|
|
|
|
}
|
2008-09-10 21:27:49 +02:00
|
|
|
|
2008-09-12 22:54:14 +02:00
|
|
|
/* Because everything is chunked and aligned to 16-bit boundaries,
|
|
|
|
* we know that most the basic node info fits into the sector. The
|
|
|
|
* associated name may not, however.
|
2020-08-09 19:40:35 +02:00
|
|
|
*
|
|
|
|
* NOTE: Since ROMFS directory entries are aligned to 16-byte boundaries,
|
|
|
|
* we are assured that ndx + ROMFS_FHDR_INFO/SIZE will lie wholly within
|
|
|
|
* the sector buffer.
|
2008-09-12 22:54:14 +02:00
|
|
|
*/
|
2008-09-10 21:27:49 +02:00
|
|
|
|
2019-10-27 18:48:14 +01:00
|
|
|
next = romfs_devread32(rm, ndx + ROMFS_FHDR_NEXT);
|
2008-09-12 22:54:14 +02:00
|
|
|
*pnext = (save & RFNEXT_OFFSETMASK) | (next & RFNEXT_ALLMODEMASK);
|
|
|
|
*pinfo = romfs_devread32(rm, ndx + ROMFS_FHDR_INFO);
|
|
|
|
*psize = romfs_devread32(rm, ndx + ROMFS_FHDR_SIZE);
|
2013-08-26 00:58:41 +02:00
|
|
|
|
2008-09-12 22:54:14 +02:00
|
|
|
return OK;
|
2008-09-10 21:27:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: romfs_parsefilename
|
|
|
|
*
|
2018-02-01 19:03:55 +01:00
|
|
|
* Description:
|
2008-09-10 21:27:49 +02:00
|
|
|
* Return the filename from directory entry at this offset
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2022-02-10 04:41:50 +01:00
|
|
|
int romfs_parsefilename(FAR struct romfs_mountpt_s *rm, uint32_t offset,
|
|
|
|
FAR char *pname)
|
2008-09-10 21:27:49 +02:00
|
|
|
{
|
2009-12-15 01:18:32 +01:00
|
|
|
int16_t ndx;
|
|
|
|
uint16_t namelen;
|
|
|
|
uint16_t chunklen;
|
|
|
|
bool done;
|
2008-09-10 21:27:49 +02:00
|
|
|
|
|
|
|
/* Loop until the whole name is obtained or until NAME_MAX characters
|
|
|
|
* of the name have been parsed.
|
|
|
|
*/
|
|
|
|
|
|
|
|
offset += ROMFS_FHDR_NAME;
|
2015-10-11 19:39:29 +02:00
|
|
|
for (namelen = 0, done = false; namelen < NAME_MAX && !done; )
|
2008-09-10 21:27:49 +02:00
|
|
|
{
|
|
|
|
/* Read the sector into memory */
|
|
|
|
|
2013-08-26 00:58:41 +02:00
|
|
|
ndx = romfs_devcacheread(rm, offset + namelen);
|
2008-09-13 00:28:11 +02:00
|
|
|
if (ndx < 0)
|
2008-09-10 21:27:49 +02:00
|
|
|
{
|
2008-09-13 00:28:11 +02:00
|
|
|
return ndx;
|
2008-09-10 21:27:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Is the name terminated in this 16-byte block */
|
|
|
|
|
|
|
|
if (rm->rm_buffer[ndx + 15] == '\0')
|
|
|
|
{
|
|
|
|
/* Yes.. then this chunk is less than 16 */
|
|
|
|
|
2015-10-11 19:39:29 +02:00
|
|
|
chunklen = strlen((FAR char *)&rm->rm_buffer[ndx]);
|
2009-12-15 01:18:32 +01:00
|
|
|
done = true;
|
2008-09-10 21:27:49 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* No.. then this chunk is 16 bytes in length */
|
|
|
|
|
|
|
|
chunklen = 16;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check if we would exceed the NAME_MAX */
|
|
|
|
|
|
|
|
if (namelen + chunklen > NAME_MAX)
|
|
|
|
{
|
|
|
|
chunklen = NAME_MAX - namelen;
|
2009-12-15 01:18:32 +01:00
|
|
|
done = true;
|
2008-09-10 21:27:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Copy the chunk */
|
|
|
|
|
|
|
|
memcpy(&pname[namelen], &rm->rm_buffer[ndx], chunklen);
|
|
|
|
namelen += chunklen;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Terminate the name (NAME_MAX+1 chars total) and return success */
|
|
|
|
|
|
|
|
pname[namelen] = '\0';
|
|
|
|
return OK;
|
|
|
|
}
|
2008-09-11 23:51:41 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: romfs_datastart
|
|
|
|
*
|
2018-02-01 19:03:55 +01:00
|
|
|
* Description:
|
2008-09-11 23:51:41 +02:00
|
|
|
* Given the offset to a file header, return the offset to the start of
|
|
|
|
* the file data
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2022-02-09 15:24:04 +01:00
|
|
|
int romfs_datastart(FAR struct romfs_mountpt_s *rm,
|
|
|
|
FAR struct romfs_nodeinfo_s *nodeinfo,
|
2022-02-10 04:41:50 +01:00
|
|
|
FAR uint32_t *start)
|
2008-09-11 23:51:41 +02:00
|
|
|
{
|
2022-02-09 15:24:04 +01:00
|
|
|
#ifdef CONFIG_FS_ROMFS_CACHE_NODE
|
|
|
|
*start = ROMFS_ALIGNUP(nodeinfo->rn_offset +
|
2022-03-01 08:37:46 +01:00
|
|
|
ROMFS_FHDR_NAME + nodeinfo->rn_namesize + 1);
|
2022-02-09 15:24:04 +01:00
|
|
|
return OK;
|
|
|
|
#else
|
|
|
|
uint32_t offset = nodeinfo->rn_offset;
|
2009-12-15 01:18:32 +01:00
|
|
|
int16_t ndx;
|
2008-09-12 21:17:15 +02:00
|
|
|
|
|
|
|
/* Loop until the header size is obtained. */
|
|
|
|
|
|
|
|
offset += ROMFS_FHDR_NAME;
|
2015-10-11 19:39:29 +02:00
|
|
|
for (; ; )
|
2008-09-12 21:17:15 +02:00
|
|
|
{
|
2008-09-11 23:51:41 +02:00
|
|
|
/* Read the sector into memory */
|
|
|
|
|
2013-09-14 20:29:17 +02:00
|
|
|
ndx = romfs_devcacheread(rm, offset);
|
2008-09-13 00:28:11 +02:00
|
|
|
if (ndx < 0)
|
2008-09-12 21:17:15 +02:00
|
|
|
{
|
2008-09-13 00:28:11 +02:00
|
|
|
return ndx;
|
2008-09-12 21:17:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Get the offset to the next chunk */
|
|
|
|
|
|
|
|
offset += 16;
|
2021-01-27 09:56:24 +01:00
|
|
|
if (offset > rm->rm_volsize)
|
2008-09-12 21:17:15 +02:00
|
|
|
{
|
|
|
|
return -EIO;
|
|
|
|
}
|
2008-09-11 23:51:41 +02:00
|
|
|
|
|
|
|
/* Is the name terminated in this 16-byte block */
|
|
|
|
|
|
|
|
if (rm->rm_buffer[ndx + 15] == '\0')
|
|
|
|
{
|
2008-09-12 21:17:15 +02:00
|
|
|
/* Yes.. then the data starts at the next chunk */
|
2008-09-11 23:51:41 +02:00
|
|
|
|
2008-09-12 21:17:15 +02:00
|
|
|
*start = offset;
|
|
|
|
return OK;
|
2008-09-11 23:51:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-09-12 21:17:15 +02:00
|
|
|
return -EINVAL; /* Won't get here */
|
2022-02-09 15:24:04 +01:00
|
|
|
#endif
|
2008-09-11 23:51:41 +02:00
|
|
|
}
|