NSH mkrd command

The typical way to create a RAM disk is to use the NuttShell (NSH) mkrd command. The syntax of that command is:

    mkrd [-m <minor>] [-s <sector-size>] <nsectors>

This command creates a ram disk consisting of <nsectors>, each of size <sector-size> (or 512 bytes if <sector-size> is not specified). The ramdisk will be registered as /dev/ram<minor>. If <minor> is not specified, mkrd will attempt to register the ram disk as /dev/ram0.

The NSH mkrd command is a simple wrapper around the OS boardctl() interface using the BOARDIOC_MKRD command. "Under the hood," this boardctl() command does the following:

  • Allocate kernel-space memory using kmm_malloc() of size <nsectors> times <sector-size>,
  • Zero the allocated memory, then
  • Call the OS-internal function ramdisk_register() to create the RAM disk.

NSH ROMFS /etc Support

A ROM disk is a block device created on a read-only file system image in FLASH or other ROM.
There is no NSH command that can be used to recreate a ROM disk. This capability can be enabled for NSH using the CONFIG_NSH_ROMFSETC as described in the section discussing NSH start-up scripts in the NSH User Guide.

Any application can create a ROM disk, however, using the boardctl() BOARDIOC_ROMDISK command.

Creating RAM Disks in Board Bring-Up Logic

RAM disks maybe created by your board-specific initialization logic that runs in supervisor mode. The board initialization function would contain logic something like the following:

    int board_ramdisk(int minor, unsigned int sectsize, unsigned int nsectors)
    {
      size_t allocsize = (size_t)sectsize * (size_t)nsectors;
      FAR uint8_t *buffer;
    
      /* Allocate the memory backing up the ramdisk */
    
      buffer = (FAR uint8_t *)kmm_zalloc(allocsize);
      if (buffer == NULL)
        {
          return -ENOMEM;
        }
    
      /* Then register the ramdisk */
    
      ret = ramdisk_register(minor, buffer, nsectors, sectsize,
                             RDFLAG_WRENABLED | RDFLAG_FUNLINK);
      if (ret < 0)
        {
          kmm_free(buffer);
        }
    
      return ret;
    }

or, equivalently, this could all be replaced with a call to the OS internal function mkrd().

Creating ROM Disks in Board Bring-Up Logic

NOTE: Currently, the romdisk_register() function is only available within the OS. There is currently logic under apps/ that violates the portable POSIX OS interface by calling romdisk_register() directly. The correct way for an application to create a ROM disk is via the boardctl(BOARDIOC_ROMDISK) command as described above. Calling romdisk_register() directly is not only a violation of the NuttX portable interface, but also cannot be done in PROTECTED or KERNEL build modes.

ROM disks, i.e., read-only disks in FLASH, would be created by the board bring-up logic in a manner similar to the way that the RAM disks were created with these caveats:

  • The FLASH region would not be allocated. Rather, the FLASH address, the sector size, and the number of sectors for the FLASH region would have to be known a priori. And
  • The API romdisk_register() would be used instead of ramdisk_register()

Perhaps like:

    int board_romdisk(int minor, FAR uint8_t *buffer, unsigned int sectsize,
                      unsigned int nsectors)
    {
      /* Register the romdisk */
    
      return romdisk_register(minor, buffer, nsectors, sectsize);
    }

Calling romdisk_register() is equivalent to calling ramdisk_register() with the final parameter flags == 0.

Most ROM disks use the ROMFS file system. CROMFS is another option. Creating the ROMFS file system images requires several steps. There are tools to make creation ROMFS ROM disks easy, but that is beyond the scope of this Wiki page. Refer instead to NuttX README.txt files such as this one.

  • No labels