1 /* ----------------------------------------------------------------------- *
3 * Copyright 2007-2008 H. Peter Anvin - All Rights Reserved
5 * Permission is hereby granted, free of charge, to any person
6 * obtaining a copy of this software and associated documentation
7 * files (the "Software"), to deal in the Software without
8 * restriction, including without limitation the rights to use,
9 * copy, modify, merge, publish, distribute, sublicense, and/or
10 * sell copies of the Software, and to permit persons to whom
11 * the Software is furnished to do so, subject to the following
14 * The above copyright notice and this permission notice shall
15 * be included in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
19 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
21 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
22 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
24 * OTHER DEALINGS IN THE SOFTWARE.
26 * ----------------------------------------------------------------------- */
31 * Utility functions to add arbitrary files including cpio header
38 #include <syslinux/linux.h>
40 #define CPIO_MAGIC "070701"
42 char c_magic
[6]; /* 070701 */
43 char c_ino
[8]; /* Inode number */
44 char c_mode
[8]; /* File mode and permissions */
45 char c_uid
[8]; /* uid */
46 char c_gid
[8]; /* gid */
47 char c_nlink
[8]; /* Number of links */
48 char c_mtime
[8]; /* Modification time */
49 char c_filesize
[8]; /* Size of data field */
50 char c_maj
[8]; /* File device major number */
51 char c_min
[8]; /* File device minor number */
52 char c_rmaj
[8]; /* Device node reference major number */
53 char c_rmin
[8]; /* Device node reference minor number */
54 char c_namesize
[8]; /* Length of filename including final \0 */
55 char c_chksum
[8]; /* Checksum if c_magic ends in 2 */
58 static uint32_t next_ino
= 1;
60 /* Create cpio headers for the directory entries leading up to a file.
61 Returns the number of bytes; doesn't touch the buffer if too small. */
62 static size_t initramfs_mkdirs(const char *filename
, void *buffer
,
65 const char *p
= filename
;
71 while ((p
= strchr(p
, '/'))) {
72 if (p
!= filename
&& p
[-1] != '/') {
74 bytes
+= ((sizeof(struct cpio_header
) + len
+ 1) + 3) & ~3;
79 if (buflen
>= bytes
) {
81 while ((p
= strchr(p
, '/'))) {
82 if (p
!= filename
&& p
[-1] != '/') {
84 bp
+= sprintf(bp
, "070701%08x%08x%08x%08x%08x%08x%08x%08x%08x"
85 "%08x%08x%08x%08x", next_ino
++, S_IFDIR
| 0755,
86 0, 0, 1, 0, 0, 0, 1, 0, 1, len
+ 1, 0);
87 memcpy(bp
, filename
, len
);
89 pad
= (-(sizeof(struct cpio_header
) + len
) & 3) + 1;
100 * Create a file header (with optional parent directory entries)
101 * and add it to an initramfs chain
103 int initramfs_mknod(struct initramfs
*ihead
, const char *filename
,
105 uint16_t mode
, size_t len
, uint32_t major
, uint32_t minor
)
108 int namelen
= strlen(filename
);
113 bytes
= initramfs_mkdirs(filename
, NULL
, 0);
117 bytes
+= ((sizeof(struct cpio_header
) + namelen
+ 1) + 3) & ~3;
119 bp
= buffer
= malloc(bytes
);
124 bp
+= initramfs_mkdirs(filename
, bp
, bytes
);
126 bp
+= sprintf(bp
, "070701%08x%08x%08x%08x%08x%08x%08x%08x%08x"
127 "%08x%08x%08x%08x", next_ino
++, mode
,
128 0, 0, 1, 0, len
, 0, 1, major
, minor
, namelen
+ 1, 0);
129 memcpy(bp
, filename
, namelen
);
131 pad
= (-(sizeof(struct cpio_header
) + namelen
) & 3) + 1;
134 if (initramfs_add_data(ihead
, buffer
, bytes
, bytes
, 4)) {
143 * Add a file given data in memory to an initramfs chain. This
144 * can be used to create nonfiles like symlinks by specifying an
147 int initramfs_add_file(struct initramfs
*ihead
, const void *data
,
148 size_t data_len
, size_t len
,
149 const char *filename
, int do_mkdir
, uint32_t mode
)
151 if (initramfs_mknod(ihead
, filename
, do_mkdir
,
152 (mode
& S_IFMT
) ? mode
: mode
| S_IFREG
, len
, 0, 1))
155 return initramfs_add_data(ihead
, data
, data_len
, len
, 4);
158 int initramfs_add_trailer(struct initramfs
*ihead
)
160 return initramfs_mknod(ihead
, "TRAILER!!!", 0, 0, 0, 0, 0);