xref: /OK3568_Linux_fs/kernel/Documentation/driver-api/early-userspace/buffer-format.rst (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun=======================
2*4882a593Smuzhiyuninitramfs buffer format
3*4882a593Smuzhiyun=======================
4*4882a593Smuzhiyun
5*4882a593SmuzhiyunAl Viro, H. Peter Anvin
6*4882a593Smuzhiyun
7*4882a593SmuzhiyunLast revision: 2002-01-13
8*4882a593Smuzhiyun
9*4882a593SmuzhiyunStarting with kernel 2.5.x, the old "initial ramdisk" protocol is
10*4882a593Smuzhiyungetting {replaced/complemented} with the new "initial ramfs"
11*4882a593Smuzhiyun(initramfs) protocol.  The initramfs contents is passed using the same
12*4882a593Smuzhiyunmemory buffer protocol used by the initrd protocol, but the contents
13*4882a593Smuzhiyunis different.  The initramfs buffer contains an archive which is
14*4882a593Smuzhiyunexpanded into a ramfs filesystem; this document details the format of
15*4882a593Smuzhiyunthe initramfs buffer format.
16*4882a593Smuzhiyun
17*4882a593SmuzhiyunThe initramfs buffer format is based around the "newc" or "crc" CPIO
18*4882a593Smuzhiyunformats, and can be created with the cpio(1) utility.  The cpio
19*4882a593Smuzhiyunarchive can be compressed using gzip(1).  One valid version of an
20*4882a593Smuzhiyuninitramfs buffer is thus a single .cpio.gz file.
21*4882a593Smuzhiyun
22*4882a593SmuzhiyunThe full format of the initramfs buffer is defined by the following
23*4882a593Smuzhiyungrammar, where::
24*4882a593Smuzhiyun
25*4882a593Smuzhiyun	*	is used to indicate "0 or more occurrences of"
26*4882a593Smuzhiyun	(|)	indicates alternatives
27*4882a593Smuzhiyun	+	indicates concatenation
28*4882a593Smuzhiyun	GZIP()	indicates the gzip(1) of the operand
29*4882a593Smuzhiyun	ALGN(n)	means padding with null bytes to an n-byte boundary
30*4882a593Smuzhiyun
31*4882a593Smuzhiyun	initramfs  := ("\0" | cpio_archive | cpio_gzip_archive)*
32*4882a593Smuzhiyun
33*4882a593Smuzhiyun	cpio_gzip_archive := GZIP(cpio_archive)
34*4882a593Smuzhiyun
35*4882a593Smuzhiyun	cpio_archive := cpio_file* + (<nothing> | cpio_trailer)
36*4882a593Smuzhiyun
37*4882a593Smuzhiyun	cpio_file := ALGN(4) + cpio_header + filename + "\0" + ALGN(4) + data
38*4882a593Smuzhiyun
39*4882a593Smuzhiyun	cpio_trailer := ALGN(4) + cpio_header + "TRAILER!!!\0" + ALGN(4)
40*4882a593Smuzhiyun
41*4882a593Smuzhiyun
42*4882a593SmuzhiyunIn human terms, the initramfs buffer contains a collection of
43*4882a593Smuzhiyuncompressed and/or uncompressed cpio archives (in the "newc" or "crc"
44*4882a593Smuzhiyunformats); arbitrary amounts zero bytes (for padding) can be added
45*4882a593Smuzhiyunbetween members.
46*4882a593Smuzhiyun
47*4882a593SmuzhiyunThe cpio "TRAILER!!!" entry (cpio end-of-archive) is optional, but is
48*4882a593Smuzhiyunnot ignored; see "handling of hard links" below.
49*4882a593Smuzhiyun
50*4882a593SmuzhiyunThe structure of the cpio_header is as follows (all fields contain
51*4882a593Smuzhiyunhexadecimal ASCII numbers fully padded with '0' on the left to the
52*4882a593Smuzhiyunfull width of the field, for example, the integer 4780 is represented
53*4882a593Smuzhiyunby the ASCII string "000012ac"):
54*4882a593Smuzhiyun
55*4882a593Smuzhiyun============= ================== ==============================================
56*4882a593SmuzhiyunField name    Field size	 Meaning
57*4882a593Smuzhiyun============= ================== ==============================================
58*4882a593Smuzhiyunc_magic	      6 bytes		 The string "070701" or "070702"
59*4882a593Smuzhiyunc_ino	      8 bytes		 File inode number
60*4882a593Smuzhiyunc_mode	      8 bytes		 File mode and permissions
61*4882a593Smuzhiyunc_uid	      8 bytes		 File uid
62*4882a593Smuzhiyunc_gid	      8 bytes		 File gid
63*4882a593Smuzhiyunc_nlink	      8 bytes		 Number of links
64*4882a593Smuzhiyunc_mtime	      8 bytes		 Modification time
65*4882a593Smuzhiyunc_filesize    8 bytes		 Size of data field
66*4882a593Smuzhiyunc_maj	      8 bytes		 Major part of file device number
67*4882a593Smuzhiyunc_min	      8 bytes		 Minor part of file device number
68*4882a593Smuzhiyunc_rmaj	      8 bytes		 Major part of device node reference
69*4882a593Smuzhiyunc_rmin	      8 bytes		 Minor part of device node reference
70*4882a593Smuzhiyunc_namesize    8 bytes		 Length of filename, including final \0
71*4882a593Smuzhiyunc_chksum      8 bytes		 Checksum of data field if c_magic is 070702;
72*4882a593Smuzhiyun				 otherwise zero
73*4882a593Smuzhiyun============= ================== ==============================================
74*4882a593Smuzhiyun
75*4882a593SmuzhiyunThe c_mode field matches the contents of st_mode returned by stat(2)
76*4882a593Smuzhiyunon Linux, and encodes the file type and file permissions.
77*4882a593Smuzhiyun
78*4882a593SmuzhiyunThe c_filesize should be zero for any file which is not a regular file
79*4882a593Smuzhiyunor symlink.
80*4882a593Smuzhiyun
81*4882a593SmuzhiyunThe c_chksum field contains a simple 32-bit unsigned sum of all the
82*4882a593Smuzhiyunbytes in the data field.  cpio(1) refers to this as "crc", which is
83*4882a593Smuzhiyunclearly incorrect (a cyclic redundancy check is a different and
84*4882a593Smuzhiyunsignificantly stronger integrity check), however, this is the
85*4882a593Smuzhiyunalgorithm used.
86*4882a593Smuzhiyun
87*4882a593SmuzhiyunIf the filename is "TRAILER!!!" this is actually an end-of-archive
88*4882a593Smuzhiyunmarker; the c_filesize for an end-of-archive marker must be zero.
89*4882a593Smuzhiyun
90*4882a593Smuzhiyun
91*4882a593SmuzhiyunHandling of hard links
92*4882a593Smuzhiyun======================
93*4882a593Smuzhiyun
94*4882a593SmuzhiyunWhen a nondirectory with c_nlink > 1 is seen, the (c_maj,c_min,c_ino)
95*4882a593Smuzhiyuntuple is looked up in a tuple buffer.  If not found, it is entered in
96*4882a593Smuzhiyunthe tuple buffer and the entry is created as usual; if found, a hard
97*4882a593Smuzhiyunlink rather than a second copy of the file is created.  It is not
98*4882a593Smuzhiyunnecessary (but permitted) to include a second copy of the file
99*4882a593Smuzhiyuncontents; if the file contents is not included, the c_filesize field
100*4882a593Smuzhiyunshould be set to zero to indicate no data section follows.  If data is
101*4882a593Smuzhiyunpresent, the previous instance of the file is overwritten; this allows
102*4882a593Smuzhiyunthe data-carrying instance of a file to occur anywhere in the sequence
103*4882a593Smuzhiyun(GNU cpio is reported to attach the data to the last instance of a
104*4882a593Smuzhiyunfile only.)
105*4882a593Smuzhiyun
106*4882a593Smuzhiyunc_filesize must not be zero for a symlink.
107*4882a593Smuzhiyun
108*4882a593SmuzhiyunWhen a "TRAILER!!!" end-of-archive marker is seen, the tuple buffer is
109*4882a593Smuzhiyunreset.  This permits archives which are generated independently to be
110*4882a593Smuzhiyunconcatenated.
111*4882a593Smuzhiyun
112*4882a593SmuzhiyunTo combine file data from different sources (without having to
113*4882a593Smuzhiyunregenerate the (c_maj,c_min,c_ino) fields), therefore, either one of
114*4882a593Smuzhiyunthe following techniques can be used:
115*4882a593Smuzhiyun
116*4882a593Smuzhiyuna) Separate the different file data sources with a "TRAILER!!!"
117*4882a593Smuzhiyun   end-of-archive marker, or
118*4882a593Smuzhiyun
119*4882a593Smuzhiyunb) Make sure c_nlink == 1 for all nondirectory entries.
120