Skip to content

Commit 23aef2d

Browse files
guohao15GUIDINGLI
authored andcommitted
romfs:extend romfs to enable write part3
add romfs_mkfs to support autoformat && forceformat Signed-off-by: guohao15 <[email protected]>
1 parent 7d8d766 commit 23aef2d

File tree

3 files changed

+171
-20
lines changed

3 files changed

+171
-20
lines changed

fs/romfs/fs_romfs.c

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1124,15 +1124,48 @@ static int romfs_bind(FAR struct inode *blkdriver, FAR const void *data,
11241124
goto errout_with_mount;
11251125
}
11261126

1127+
#ifdef CONFIG_FS_ROMFS_WRITEABLE
1128+
if (data && strstr(data, "rw") && strstr(data, "forceformat"))
1129+
{
1130+
ret = romfs_mkfs(rm);
1131+
if (ret < 0)
1132+
{
1133+
ferr("ERROR: romfs_mkfs failed: %d\n", ret);
1134+
goto errout_with_buffer;
1135+
}
1136+
}
1137+
#endif
1138+
11271139
/* Then complete the mount by getting the ROMFS configuratrion from
11281140
* the ROMF header
11291141
*/
11301142

11311143
ret = romfs_fsconfigure(rm, data);
11321144
if (ret < 0)
11331145
{
1134-
ferr("ERROR: romfs_fsconfigure failed: %d\n", ret);
1135-
goto errout_with_buffer;
1146+
#ifdef CONFIG_FS_ROMFS_WRITEABLE
1147+
if (data && strstr(data, "rw") && strstr(data, "autoformat"))
1148+
{
1149+
ret = romfs_mkfs(rm);
1150+
if (ret < 0)
1151+
{
1152+
ferr("ERROR: romfs_format failed: %d\n", ret);
1153+
goto errout_with_buffer;
1154+
}
1155+
1156+
ret = romfs_fsconfigure(rm, data);
1157+
if (ret < 0)
1158+
{
1159+
ferr("ERROR: romfs_fsconfigure failed: %d\n", ret);
1160+
goto errout_with_buffer;
1161+
}
1162+
}
1163+
else
1164+
#endif
1165+
{
1166+
ferr("ERROR: romfs_fsconfigure failed: %d\n", ret);
1167+
goto errout_with_buffer;
1168+
}
11361169
}
11371170

11381171
/* Mounted! */
@@ -1141,10 +1174,7 @@ static int romfs_bind(FAR struct inode *blkdriver, FAR const void *data,
11411174
return OK;
11421175

11431176
errout_with_buffer:
1144-
if (!rm->rm_xipbase)
1145-
{
1146-
fs_heap_free(rm->rm_buffer);
1147-
}
1177+
fs_heap_free(rm->rm_devbuffer);
11481178

11491179
errout_with_mount:
11501180
nxrmutex_destroy(&rm->rm_lock);
@@ -1231,10 +1261,7 @@ static int romfs_unbind(FAR void *handle, FAR struct inode **blkdriver,
12311261

12321262
/* Release the mountpoint private data */
12331263

1234-
if (!rm->rm_xipbase)
1235-
{
1236-
fs_heap_free(rm->rm_buffer);
1237-
}
1264+
fs_heap_free(rm->rm_devbuffer);
12381265

12391266
#ifdef CONFIG_FS_ROMFS_CACHE_NODE
12401267
romfs_freenode(rm->rm_root);

fs/romfs/fs_romfs.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ struct romfs_mountpt_s
154154
uint32_t rm_cachesector; /* Current sector in the rm_buffer */
155155
FAR uint8_t *rm_xipbase; /* Base address of directly accessible media */
156156
FAR uint8_t *rm_buffer; /* Device sector buffer, allocated if rm_xipbase==0 */
157+
FAR uint8_t *rm_devbuffer; /* Device sector buffer, allocated for write if rm_xipbase != 0 */
157158
#ifdef CONFIG_FS_ROMFS_WRITEABLE
158159
struct list_node rm_sparelist; /* The list of spare space */
159160
#endif
@@ -231,6 +232,7 @@ int romfs_datastart(FAR struct romfs_mountpt_s *rm,
231232
void romfs_freenode(FAR struct romfs_nodeinfo_s *node);
232233
#endif
233234
#ifdef CONFIG_FS_ROMFS_WRITEABLE
235+
int romfs_mkfs(FAR struct romfs_mountpt_s *rm);
234236
void romfs_free_sparelist(FAR struct list_node *list);
235237
#endif
236238

fs/romfs/fs_romfsutil.c

Lines changed: 132 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,7 @@ static FAR struct romfs_sparenode_s *
408408
romfs_alloc_sparenode(uint32_t start, uint32_t end)
409409
{
410410
FAR struct romfs_sparenode_s *node;
411-
node = kmm_malloc(sizeof(struct romfs_sparenode_s));
411+
node = fs_heap_malloc(sizeof(struct romfs_sparenode_s));
412412
if (node == NULL)
413413
{
414414
ferr("romfs_alloc_sparenode: no memory\n");
@@ -474,7 +474,7 @@ static int romfs_alloc_spareregion(FAR struct list_node *list,
474474
/* Delete the node */
475475

476476
list_delete(&node->node);
477-
kmm_free(node);
477+
fs_heap_free(node);
478478
return 0;
479479
}
480480
else if (start == node->start)
@@ -514,6 +514,78 @@ static int romfs_alloc_spareregion(FAR struct list_node *list,
514514
end);
515515
return -ENOENT;
516516
}
517+
518+
/****************************************************************************
519+
* Name: romfs_devwrite32
520+
*
521+
* Description:
522+
* Write the big-endian 32-bit value to the mount device buffer
523+
*
524+
****************************************************************************/
525+
526+
static void romfs_devwrite32(FAR struct romfs_mountpt_s *rm,
527+
int ndx, uint32_t value)
528+
{
529+
/* Write the 32-bit value to the specified index in the buffer */
530+
531+
rm->rm_devbuffer[ndx] = (uint8_t)(value >> 24) & 0xff;
532+
rm->rm_devbuffer[ndx + 1] = (uint8_t)(value >> 16) & 0xff;
533+
rm->rm_devbuffer[ndx + 2] = (uint8_t)(value >> 8) & 0xff;
534+
rm->rm_devbuffer[ndx + 3] = (uint8_t)(value & 0xff);
535+
}
536+
537+
/****************************************************************************
538+
* Name: romfs_hwwrite
539+
*
540+
* Description:
541+
* Write the specified number of sectors to the block device
542+
*
543+
****************************************************************************/
544+
545+
static int romfs_hwwrite(FAR struct romfs_mountpt_s *rm, FAR uint8_t *buffer,
546+
uint32_t sector, unsigned int nsectors)
547+
{
548+
FAR struct inode *inode = rm->rm_blkdriver;
549+
ssize_t ret = -ENODEV;
550+
551+
if (inode->u.i_bops->write)
552+
{
553+
ret = inode->u.i_bops->write(inode, buffer, sector, nsectors);
554+
}
555+
556+
if (ret == (ssize_t)nsectors)
557+
{
558+
ret = OK;
559+
}
560+
561+
return ret;
562+
}
563+
564+
/****************************************************************************
565+
* Name: romfs_devcachewrite
566+
*
567+
* Description:
568+
* Write the specified sector for specified offset into the sector cache.
569+
*
570+
****************************************************************************/
571+
572+
static int romfs_devcachewrite(FAR struct romfs_mountpt_s *rm,
573+
uint32_t sector)
574+
{
575+
int ret;
576+
577+
ret = romfs_hwwrite(rm, rm->rm_devbuffer, sector, 1);
578+
if (ret == OK)
579+
{
580+
rm->rm_cachesector = sector;
581+
}
582+
else
583+
{
584+
rm->rm_cachesector = (uint32_t)-1;
585+
}
586+
587+
return ret;
588+
}
517589
#endif
518590

519591
/****************************************************************************
@@ -800,6 +872,14 @@ int romfs_hwconfigure(FAR struct romfs_mountpt_s *rm)
800872

801873
rm->rm_cachesector = (uint32_t)-1;
802874

875+
/* Allocate the device cache buffer for normal sector accesses */
876+
877+
rm->rm_devbuffer = fs_heap_malloc(rm->rm_hwsectorsize);
878+
if (!rm->rm_devbuffer)
879+
{
880+
return -ENOMEM;
881+
}
882+
803883
if (inode->u.i_bops->ioctl)
804884
{
805885
ret = inode->u.i_bops->ioctl(inode, BIOC_XIPBASE,
@@ -816,14 +896,9 @@ int romfs_hwconfigure(FAR struct romfs_mountpt_s *rm)
816896
}
817897
}
818898

819-
/* Allocate the device cache buffer for normal sector accesses */
820-
821-
rm->rm_buffer = fs_heap_malloc(rm->rm_hwsectorsize);
822-
if (!rm->rm_buffer)
823-
{
824-
return -ENOMEM;
825-
}
899+
/* The device cache buffer for normal sector accesses */
826900

901+
rm->rm_buffer = rm->rm_devbuffer;
827902
return OK;
828903
}
829904

@@ -843,7 +918,7 @@ void romfs_free_sparelist(FAR struct list_node *list)
843918
list_for_every_entry_safe(list, node, tmp, struct romfs_sparenode_s, node)
844919
{
845920
list_delete(&node->node);
846-
kmm_free(node);
921+
fs_heap_free(node);
847922
}
848923
}
849924
#endif
@@ -1343,3 +1418,50 @@ int romfs_datastart(FAR struct romfs_mountpt_s *rm,
13431418
return -EINVAL; /* Won't get here */
13441419
#endif
13451420
}
1421+
1422+
#ifdef CONFIG_FS_ROMFS_WRITEABLE
1423+
1424+
/****************************************************************************
1425+
* Name: romfs_mkfs
1426+
*
1427+
* Description:
1428+
* Format the romfs filesystem
1429+
*
1430+
****************************************************************************/
1431+
1432+
int romfs_mkfs(FAR struct romfs_mountpt_s *rm)
1433+
{
1434+
/* Write the magic number at that identifies this as a ROMFS filesystem */
1435+
1436+
memcpy(rm->rm_devbuffer + ROMFS_VHDR_ROM1FS, ROMFS_VHDR_MAGIC,
1437+
ROMFS_VHDR_SIZE);
1438+
1439+
/* Init the ROMFS volume to zero */
1440+
1441+
romfs_devwrite32(rm, ROMFS_VHDR_SIZE, 0);
1442+
1443+
/* Write the volume name */
1444+
1445+
memcpy(rm->rm_devbuffer + ROMFS_VHDR_VOLNAME, "romfs", 6);
1446+
1447+
/* Write the root node . */
1448+
1449+
romfs_devwrite32(rm, 0x20 + ROMFS_FHDR_NEXT, 0x40 | RFNEXT_DIRECTORY);
1450+
romfs_devwrite32(rm, 0x20 + ROMFS_FHDR_INFO, 0x20);
1451+
romfs_devwrite32(rm, 0x20 + ROMFS_FHDR_SIZE, 0);
1452+
romfs_devwrite32(rm, 0x20 + ROMFS_FHDR_CHKSUM, 0);
1453+
memcpy(rm->rm_devbuffer + 0x20 + ROMFS_FHDR_NAME, ".", 2);
1454+
1455+
/* Write the root node .. */
1456+
1457+
romfs_devwrite32(rm, 0x40 + ROMFS_FHDR_NEXT, RFNEXT_HARDLINK);
1458+
romfs_devwrite32(rm, 0x40 + ROMFS_FHDR_INFO, 0x20);
1459+
romfs_devwrite32(rm, 0x40 + ROMFS_FHDR_SIZE, 0);
1460+
romfs_devwrite32(rm, 0x40 + ROMFS_FHDR_CHKSUM, 0);
1461+
memcpy(rm->rm_devbuffer + 0x40 + ROMFS_FHDR_NAME, "..", 3);
1462+
1463+
/* Write the buffer to sector zero */
1464+
1465+
return romfs_devcachewrite(rm, 0);
1466+
}
1467+
#endif

0 commit comments

Comments
 (0)