Nios II Classic Software Developer’s Handbook

ID 683282
Date 5/14/2015
Public
Document Table of Contents

6.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.

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 > Altera > Nios II > Nios II Documentation.