162306a36Sopenharmony_ci=======================
262306a36Sopenharmony_ciinitramfs buffer format
362306a36Sopenharmony_ci=======================
462306a36Sopenharmony_ci
562306a36Sopenharmony_ciAl Viro, H. Peter Anvin
662306a36Sopenharmony_ci
762306a36Sopenharmony_ciLast revision: 2002-01-13
862306a36Sopenharmony_ci
962306a36Sopenharmony_ciStarting with kernel 2.5.x, the old "initial ramdisk" protocol is
1062306a36Sopenharmony_cigetting {replaced/complemented} with the new "initial ramfs"
1162306a36Sopenharmony_ci(initramfs) protocol.  The initramfs contents is passed using the same
1262306a36Sopenharmony_cimemory buffer protocol used by the initrd protocol, but the contents
1362306a36Sopenharmony_ciis different.  The initramfs buffer contains an archive which is
1462306a36Sopenharmony_ciexpanded into a ramfs filesystem; this document details the format of
1562306a36Sopenharmony_cithe initramfs buffer format.
1662306a36Sopenharmony_ci
1762306a36Sopenharmony_ciThe initramfs buffer format is based around the "newc" or "crc" CPIO
1862306a36Sopenharmony_ciformats, and can be created with the cpio(1) utility.  The cpio
1962306a36Sopenharmony_ciarchive can be compressed using gzip(1).  One valid version of an
2062306a36Sopenharmony_ciinitramfs buffer is thus a single .cpio.gz file.
2162306a36Sopenharmony_ci
2262306a36Sopenharmony_ciThe full format of the initramfs buffer is defined by the following
2362306a36Sopenharmony_cigrammar, where::
2462306a36Sopenharmony_ci
2562306a36Sopenharmony_ci	*	is used to indicate "0 or more occurrences of"
2662306a36Sopenharmony_ci	(|)	indicates alternatives
2762306a36Sopenharmony_ci	+	indicates concatenation
2862306a36Sopenharmony_ci	GZIP()	indicates the gzip(1) of the operand
2962306a36Sopenharmony_ci	ALGN(n)	means padding with null bytes to an n-byte boundary
3062306a36Sopenharmony_ci
3162306a36Sopenharmony_ci	initramfs  := ("\0" | cpio_archive | cpio_gzip_archive)*
3262306a36Sopenharmony_ci
3362306a36Sopenharmony_ci	cpio_gzip_archive := GZIP(cpio_archive)
3462306a36Sopenharmony_ci
3562306a36Sopenharmony_ci	cpio_archive := cpio_file* + (<nothing> | cpio_trailer)
3662306a36Sopenharmony_ci
3762306a36Sopenharmony_ci	cpio_file := ALGN(4) + cpio_header + filename + "\0" + ALGN(4) + data
3862306a36Sopenharmony_ci
3962306a36Sopenharmony_ci	cpio_trailer := ALGN(4) + cpio_header + "TRAILER!!!\0" + ALGN(4)
4062306a36Sopenharmony_ci
4162306a36Sopenharmony_ci
4262306a36Sopenharmony_ciIn human terms, the initramfs buffer contains a collection of
4362306a36Sopenharmony_cicompressed and/or uncompressed cpio archives (in the "newc" or "crc"
4462306a36Sopenharmony_ciformats); arbitrary amounts zero bytes (for padding) can be added
4562306a36Sopenharmony_cibetween members.
4662306a36Sopenharmony_ci
4762306a36Sopenharmony_ciThe cpio "TRAILER!!!" entry (cpio end-of-archive) is optional, but is
4862306a36Sopenharmony_cinot ignored; see "handling of hard links" below.
4962306a36Sopenharmony_ci
5062306a36Sopenharmony_ciThe structure of the cpio_header is as follows (all fields contain
5162306a36Sopenharmony_cihexadecimal ASCII numbers fully padded with '0' on the left to the
5262306a36Sopenharmony_cifull width of the field, for example, the integer 4780 is represented
5362306a36Sopenharmony_ciby the ASCII string "000012ac"):
5462306a36Sopenharmony_ci
5562306a36Sopenharmony_ci============= ================== ==============================================
5662306a36Sopenharmony_ciField name    Field size	 Meaning
5762306a36Sopenharmony_ci============= ================== ==============================================
5862306a36Sopenharmony_cic_magic	      6 bytes		 The string "070701" or "070702"
5962306a36Sopenharmony_cic_ino	      8 bytes		 File inode number
6062306a36Sopenharmony_cic_mode	      8 bytes		 File mode and permissions
6162306a36Sopenharmony_cic_uid	      8 bytes		 File uid
6262306a36Sopenharmony_cic_gid	      8 bytes		 File gid
6362306a36Sopenharmony_cic_nlink	      8 bytes		 Number of links
6462306a36Sopenharmony_cic_mtime	      8 bytes		 Modification time
6562306a36Sopenharmony_cic_filesize    8 bytes		 Size of data field
6662306a36Sopenharmony_cic_maj	      8 bytes		 Major part of file device number
6762306a36Sopenharmony_cic_min	      8 bytes		 Minor part of file device number
6862306a36Sopenharmony_cic_rmaj	      8 bytes		 Major part of device node reference
6962306a36Sopenharmony_cic_rmin	      8 bytes		 Minor part of device node reference
7062306a36Sopenharmony_cic_namesize    8 bytes		 Length of filename, including final \0
7162306a36Sopenharmony_cic_chksum      8 bytes		 Checksum of data field if c_magic is 070702;
7262306a36Sopenharmony_ci				 otherwise zero
7362306a36Sopenharmony_ci============= ================== ==============================================
7462306a36Sopenharmony_ci
7562306a36Sopenharmony_ciThe c_mode field matches the contents of st_mode returned by stat(2)
7662306a36Sopenharmony_cion Linux, and encodes the file type and file permissions.
7762306a36Sopenharmony_ci
7862306a36Sopenharmony_ciThe c_filesize should be zero for any file which is not a regular file
7962306a36Sopenharmony_cior symlink.
8062306a36Sopenharmony_ci
8162306a36Sopenharmony_ciThe c_chksum field contains a simple 32-bit unsigned sum of all the
8262306a36Sopenharmony_cibytes in the data field.  cpio(1) refers to this as "crc", which is
8362306a36Sopenharmony_ciclearly incorrect (a cyclic redundancy check is a different and
8462306a36Sopenharmony_cisignificantly stronger integrity check), however, this is the
8562306a36Sopenharmony_cialgorithm used.
8662306a36Sopenharmony_ci
8762306a36Sopenharmony_ciIf the filename is "TRAILER!!!" this is actually an end-of-archive
8862306a36Sopenharmony_cimarker; the c_filesize for an end-of-archive marker must be zero.
8962306a36Sopenharmony_ci
9062306a36Sopenharmony_ci
9162306a36Sopenharmony_ciHandling of hard links
9262306a36Sopenharmony_ci======================
9362306a36Sopenharmony_ci
9462306a36Sopenharmony_ciWhen a nondirectory with c_nlink > 1 is seen, the (c_maj,c_min,c_ino)
9562306a36Sopenharmony_cituple is looked up in a tuple buffer.  If not found, it is entered in
9662306a36Sopenharmony_cithe tuple buffer and the entry is created as usual; if found, a hard
9762306a36Sopenharmony_cilink rather than a second copy of the file is created.  It is not
9862306a36Sopenharmony_cinecessary (but permitted) to include a second copy of the file
9962306a36Sopenharmony_cicontents; if the file contents is not included, the c_filesize field
10062306a36Sopenharmony_cishould be set to zero to indicate no data section follows.  If data is
10162306a36Sopenharmony_cipresent, the previous instance of the file is overwritten; this allows
10262306a36Sopenharmony_cithe data-carrying instance of a file to occur anywhere in the sequence
10362306a36Sopenharmony_ci(GNU cpio is reported to attach the data to the last instance of a
10462306a36Sopenharmony_cifile only.)
10562306a36Sopenharmony_ci
10662306a36Sopenharmony_cic_filesize must not be zero for a symlink.
10762306a36Sopenharmony_ci
10862306a36Sopenharmony_ciWhen a "TRAILER!!!" end-of-archive marker is seen, the tuple buffer is
10962306a36Sopenharmony_cireset.  This permits archives which are generated independently to be
11062306a36Sopenharmony_ciconcatenated.
11162306a36Sopenharmony_ci
11262306a36Sopenharmony_ciTo combine file data from different sources (without having to
11362306a36Sopenharmony_ciregenerate the (c_maj,c_min,c_ino) fields), therefore, either one of
11462306a36Sopenharmony_cithe following techniques can be used:
11562306a36Sopenharmony_ci
11662306a36Sopenharmony_cia) Separate the different file data sources with a "TRAILER!!!"
11762306a36Sopenharmony_ci   end-of-archive marker, or
11862306a36Sopenharmony_ci
11962306a36Sopenharmony_cib) Make sure c_nlink == 1 for all nondirectory entries.
120