Nios® II Software Developer Handbook

ID 683525
Date 8/28/2023
Public
Document Table of Contents

7.6. File System

The HAL provides infrastructure for UNIX-style file access. You can use this infrastructure to build a file system on any storage devices available in your hardware.

For more information, refer to an example in the "Read-Only Zip File System" chapter.

You can access files in a HAL-based file system by using either the C standard library file I/O functions in the newlib C library (for example fopen(), fclose(), and fread()), or using the UNIX-style file I/O provided by the HAL.

The HAL provides the following UNIX-style functions for file manipulation:

  • close()
  • fstat()
  • ioctl()
  • isatty()
  • lseek()
  • open()
  • read()
  • stat()
  • write()

For more information about these functions, refer to the "HAL API Reference" chapter.

The HAL registers a file subsystem as a mount point in the global HAL file system. Attempts to access files below that mount point are directed to the file subsystem. For example, if a read-only zip file subsystem (zipfs) is mounted as /mount/zipfs0, the zipfs file subsystem handles calls to fopen() for /mount/zipfs0/myfile.

There is no concept of a current directory. Software must access all files using absolute paths.

The HAL file infrastructure also allows you to manipulate character mode devices with UNIX-style path names. The HAL registers character mode devices as nodes in the HAL file system. By convention, system.h defines the name of a device node as the prefix /dev/ plus the name assigned to the hardware component at system generation time. For example, a UART peripheral that appears as uart1 in Platform Designer is named /dev/uart1 in system.h.

Note: The standard header files stdio.h, stddef.h, and stdlib.h are installed with the HAL.
#include <stdio.h>
#include <stddef.h>
#include <stdlib.h>
#define BUF_SIZE (10)
int main(void)
{
   FILE* fp;
   char buffer[BUF_SIZE];
   fp = fopen ("/mount/rozipfs/test", "r"); if (fp == NULL)
   {
      printf ("Cannot open file.\n");
      exit (1);
   }
   fread (buffer, BUF_SIZE, 1, fp);
   fclose (fp);
   return 0;
}

For more information about the use of these functions, refer to the newlib C library documentation installed with the Nios® II EDS. To access the documentation, go to the Windows Start menu, click Programs > Intel FPGA > Nios® II > Nios® II Documentation.