xref: /openbmc/linux/Documentation/driver-api/early-userspace/buffer-format.rst (revision 0898782247ae533d1f4e47a06bc5d4870931b284)
1*ec4b78a0SMauro Carvalho Chehab=======================
2*ec4b78a0SMauro Carvalho Chehabinitramfs buffer format
3*ec4b78a0SMauro Carvalho Chehab=======================
4*ec4b78a0SMauro Carvalho Chehab
5*ec4b78a0SMauro Carvalho ChehabAl Viro, H. Peter Anvin
6*ec4b78a0SMauro Carvalho Chehab
7*ec4b78a0SMauro Carvalho ChehabLast revision: 2002-01-13
8*ec4b78a0SMauro Carvalho Chehab
9*ec4b78a0SMauro Carvalho ChehabStarting with kernel 2.5.x, the old "initial ramdisk" protocol is
10*ec4b78a0SMauro Carvalho Chehabgetting {replaced/complemented} with the new "initial ramfs"
11*ec4b78a0SMauro Carvalho Chehab(initramfs) protocol.  The initramfs contents is passed using the same
12*ec4b78a0SMauro Carvalho Chehabmemory buffer protocol used by the initrd protocol, but the contents
13*ec4b78a0SMauro Carvalho Chehabis different.  The initramfs buffer contains an archive which is
14*ec4b78a0SMauro Carvalho Chehabexpanded into a ramfs filesystem; this document details the format of
15*ec4b78a0SMauro Carvalho Chehabthe initramfs buffer format.
16*ec4b78a0SMauro Carvalho Chehab
17*ec4b78a0SMauro Carvalho ChehabThe initramfs buffer format is based around the "newc" or "crc" CPIO
18*ec4b78a0SMauro Carvalho Chehabformats, and can be created with the cpio(1) utility.  The cpio
19*ec4b78a0SMauro Carvalho Chehabarchive can be compressed using gzip(1).  One valid version of an
20*ec4b78a0SMauro Carvalho Chehabinitramfs buffer is thus a single .cpio.gz file.
21*ec4b78a0SMauro Carvalho Chehab
22*ec4b78a0SMauro Carvalho ChehabThe full format of the initramfs buffer is defined by the following
23*ec4b78a0SMauro Carvalho Chehabgrammar, where::
24*ec4b78a0SMauro Carvalho Chehab
25*ec4b78a0SMauro Carvalho Chehab	*	is used to indicate "0 or more occurrences of"
26*ec4b78a0SMauro Carvalho Chehab	(|)	indicates alternatives
27*ec4b78a0SMauro Carvalho Chehab	+	indicates concatenation
28*ec4b78a0SMauro Carvalho Chehab	GZIP()	indicates the gzip(1) of the operand
29*ec4b78a0SMauro Carvalho Chehab	ALGN(n)	means padding with null bytes to an n-byte boundary
30*ec4b78a0SMauro Carvalho Chehab
31*ec4b78a0SMauro Carvalho Chehab	initramfs  := ("\0" | cpio_archive | cpio_gzip_archive)*
32*ec4b78a0SMauro Carvalho Chehab
33*ec4b78a0SMauro Carvalho Chehab	cpio_gzip_archive := GZIP(cpio_archive)
34*ec4b78a0SMauro Carvalho Chehab
35*ec4b78a0SMauro Carvalho Chehab	cpio_archive := cpio_file* + (<nothing> | cpio_trailer)
36*ec4b78a0SMauro Carvalho Chehab
37*ec4b78a0SMauro Carvalho Chehab	cpio_file := ALGN(4) + cpio_header + filename + "\0" + ALGN(4) + data
38*ec4b78a0SMauro Carvalho Chehab
39*ec4b78a0SMauro Carvalho Chehab	cpio_trailer := ALGN(4) + cpio_header + "TRAILER!!!\0" + ALGN(4)
40*ec4b78a0SMauro Carvalho Chehab
41*ec4b78a0SMauro Carvalho Chehab
42*ec4b78a0SMauro Carvalho ChehabIn human terms, the initramfs buffer contains a collection of
43*ec4b78a0SMauro Carvalho Chehabcompressed and/or uncompressed cpio archives (in the "newc" or "crc"
44*ec4b78a0SMauro Carvalho Chehabformats); arbitrary amounts zero bytes (for padding) can be added
45*ec4b78a0SMauro Carvalho Chehabbetween members.
46*ec4b78a0SMauro Carvalho Chehab
47*ec4b78a0SMauro Carvalho ChehabThe cpio "TRAILER!!!" entry (cpio end-of-archive) is optional, but is
48*ec4b78a0SMauro Carvalho Chehabnot ignored; see "handling of hard links" below.
49*ec4b78a0SMauro Carvalho Chehab
50*ec4b78a0SMauro Carvalho ChehabThe structure of the cpio_header is as follows (all fields contain
51*ec4b78a0SMauro Carvalho Chehabhexadecimal ASCII numbers fully padded with '0' on the left to the
52*ec4b78a0SMauro Carvalho Chehabfull width of the field, for example, the integer 4780 is represented
53*ec4b78a0SMauro Carvalho Chehabby the ASCII string "000012ac"):
54*ec4b78a0SMauro Carvalho Chehab
55*ec4b78a0SMauro Carvalho Chehab============= ================== ==============================================
56*ec4b78a0SMauro Carvalho ChehabField name    Field size	 Meaning
57*ec4b78a0SMauro Carvalho Chehab============= ================== ==============================================
58*ec4b78a0SMauro Carvalho Chehabc_magic	      6 bytes		 The string "070701" or "070702"
59*ec4b78a0SMauro Carvalho Chehabc_ino	      8 bytes		 File inode number
60*ec4b78a0SMauro Carvalho Chehabc_mode	      8 bytes		 File mode and permissions
61*ec4b78a0SMauro Carvalho Chehabc_uid	      8 bytes		 File uid
62*ec4b78a0SMauro Carvalho Chehabc_gid	      8 bytes		 File gid
63*ec4b78a0SMauro Carvalho Chehabc_nlink	      8 bytes		 Number of links
64*ec4b78a0SMauro Carvalho Chehabc_mtime	      8 bytes		 Modification time
65*ec4b78a0SMauro Carvalho Chehabc_filesize    8 bytes		 Size of data field
66*ec4b78a0SMauro Carvalho Chehabc_maj	      8 bytes		 Major part of file device number
67*ec4b78a0SMauro Carvalho Chehabc_min	      8 bytes		 Minor part of file device number
68*ec4b78a0SMauro Carvalho Chehabc_rmaj	      8 bytes		 Major part of device node reference
69*ec4b78a0SMauro Carvalho Chehabc_rmin	      8 bytes		 Minor part of device node reference
70*ec4b78a0SMauro Carvalho Chehabc_namesize    8 bytes		 Length of filename, including final \0
71*ec4b78a0SMauro Carvalho Chehabc_chksum      8 bytes		 Checksum of data field if c_magic is 070702;
72*ec4b78a0SMauro Carvalho Chehab				 otherwise zero
73*ec4b78a0SMauro Carvalho Chehab============= ================== ==============================================
74*ec4b78a0SMauro Carvalho Chehab
75*ec4b78a0SMauro Carvalho ChehabThe c_mode field matches the contents of st_mode returned by stat(2)
76*ec4b78a0SMauro Carvalho Chehabon Linux, and encodes the file type and file permissions.
77*ec4b78a0SMauro Carvalho Chehab
78*ec4b78a0SMauro Carvalho ChehabThe c_filesize should be zero for any file which is not a regular file
79*ec4b78a0SMauro Carvalho Chehabor symlink.
80*ec4b78a0SMauro Carvalho Chehab
81*ec4b78a0SMauro Carvalho ChehabThe c_chksum field contains a simple 32-bit unsigned sum of all the
82*ec4b78a0SMauro Carvalho Chehabbytes in the data field.  cpio(1) refers to this as "crc", which is
83*ec4b78a0SMauro Carvalho Chehabclearly incorrect (a cyclic redundancy check is a different and
84*ec4b78a0SMauro Carvalho Chehabsignificantly stronger integrity check), however, this is the
85*ec4b78a0SMauro Carvalho Chehabalgorithm used.
86*ec4b78a0SMauro Carvalho Chehab
87*ec4b78a0SMauro Carvalho ChehabIf the filename is "TRAILER!!!" this is actually an end-of-archive
88*ec4b78a0SMauro Carvalho Chehabmarker; the c_filesize for an end-of-archive marker must be zero.
89*ec4b78a0SMauro Carvalho Chehab
90*ec4b78a0SMauro Carvalho Chehab
91*ec4b78a0SMauro Carvalho ChehabHandling of hard links
92*ec4b78a0SMauro Carvalho Chehab======================
93*ec4b78a0SMauro Carvalho Chehab
94*ec4b78a0SMauro Carvalho ChehabWhen a nondirectory with c_nlink > 1 is seen, the (c_maj,c_min,c_ino)
95*ec4b78a0SMauro Carvalho Chehabtuple is looked up in a tuple buffer.  If not found, it is entered in
96*ec4b78a0SMauro Carvalho Chehabthe tuple buffer and the entry is created as usual; if found, a hard
97*ec4b78a0SMauro Carvalho Chehablink rather than a second copy of the file is created.  It is not
98*ec4b78a0SMauro Carvalho Chehabnecessary (but permitted) to include a second copy of the file
99*ec4b78a0SMauro Carvalho Chehabcontents; if the file contents is not included, the c_filesize field
100*ec4b78a0SMauro Carvalho Chehabshould be set to zero to indicate no data section follows.  If data is
101*ec4b78a0SMauro Carvalho Chehabpresent, the previous instance of the file is overwritten; this allows
102*ec4b78a0SMauro Carvalho Chehabthe data-carrying instance of a file to occur anywhere in the sequence
103*ec4b78a0SMauro Carvalho Chehab(GNU cpio is reported to attach the data to the last instance of a
104*ec4b78a0SMauro Carvalho Chehabfile only.)
105*ec4b78a0SMauro Carvalho Chehab
106*ec4b78a0SMauro Carvalho Chehabc_filesize must not be zero for a symlink.
107*ec4b78a0SMauro Carvalho Chehab
108*ec4b78a0SMauro Carvalho ChehabWhen a "TRAILER!!!" end-of-archive marker is seen, the tuple buffer is
109*ec4b78a0SMauro Carvalho Chehabreset.  This permits archives which are generated independently to be
110*ec4b78a0SMauro Carvalho Chehabconcatenated.
111*ec4b78a0SMauro Carvalho Chehab
112*ec4b78a0SMauro Carvalho ChehabTo combine file data from different sources (without having to
113*ec4b78a0SMauro Carvalho Chehabregenerate the (c_maj,c_min,c_ino) fields), therefore, either one of
114*ec4b78a0SMauro Carvalho Chehabthe following techniques can be used:
115*ec4b78a0SMauro Carvalho Chehab
116*ec4b78a0SMauro Carvalho Chehaba) Separate the different file data sources with a "TRAILER!!!"
117*ec4b78a0SMauro Carvalho Chehab   end-of-archive marker, or
118*ec4b78a0SMauro Carvalho Chehab
119*ec4b78a0SMauro Carvalho Chehabb) Make sure c_nlink == 1 for all nondirectory entries.
120