Adding upstream version 4.00~pre54+dfsg.
[syslinux-debian/hramrach.git] / com32 / lib / syslinux / initramfs_file.c
blob763eff2835dd18ac68b7d60ae6545011d6f64561
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
12 * conditions:
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 * ----------------------------------------------------------------------- */
29 * initramfs_file.c
31 * Utility functions to add arbitrary files including cpio header
34 #include <stdlib.h>
35 #include <stdio.h>
36 #include <string.h>
37 #include <sys/stat.h>
38 #include <syslinux/linux.h>
40 #define CPIO_MAGIC "070701"
41 struct cpio_header {
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,
63 size_t buflen)
65 const char *p = filename;
66 char *bp = buffer;
67 int len;
68 size_t bytes = 0;
69 int pad;
71 while ((p = strchr(p, '/'))) {
72 if (p != filename && p[-1] != '/') {
73 len = p - filename;
74 bytes += ((sizeof(struct cpio_header) + len + 1) + 3) & ~3;
76 p++;
79 if (buflen >= bytes) {
80 p = filename;
81 while ((p = strchr(p, '/'))) {
82 if (p != filename && p[-1] != '/') {
83 len = p - filename;
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);
88 bp += len;
89 pad = (-(sizeof(struct cpio_header) + len) & 3) + 1;
90 memset(bp, 0, pad);
91 bp += pad;
96 return bytes;
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,
104 int do_mkdir,
105 uint16_t mode, size_t len, uint32_t major, uint32_t minor)
107 size_t bytes;
108 int namelen = strlen(filename);
109 int pad;
110 char *buffer, *bp;
112 if (do_mkdir)
113 bytes = initramfs_mkdirs(filename, NULL, 0);
114 else
115 bytes = 0;
117 bytes += ((sizeof(struct cpio_header) + namelen + 1) + 3) & ~3;
119 bp = buffer = malloc(bytes);
120 if (!buffer)
121 return -1;
123 if (do_mkdir)
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);
130 bp += len;
131 pad = (-(sizeof(struct cpio_header) + namelen) & 3) + 1;
132 memset(bp, 0, pad);
134 if (initramfs_add_data(ihead, buffer, bytes, bytes, 4)) {
135 free(buffer);
136 return -1;
139 return 0;
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
145 * appropriate mode.
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))
153 return -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);