summaryrefslogtreecommitdiff
path: root/com32/lib/syslinux/initramfs_file.c
blob: 763eff2835dd18ac68b7d60ae6545011d6f64561 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/* ----------------------------------------------------------------------- *
 *
 *   Copyright 2007-2008 H. Peter Anvin - All Rights Reserved
 *
 *   Permission is hereby granted, free of charge, to any person
 *   obtaining a copy of this software and associated documentation
 *   files (the "Software"), to deal in the Software without
 *   restriction, including without limitation the rights to use,
 *   copy, modify, merge, publish, distribute, sublicense, and/or
 *   sell copies of the Software, and to permit persons to whom
 *   the Software is furnished to do so, subject to the following
 *   conditions:
 *
 *   The above copyright notice and this permission notice shall
 *   be included in all copies or substantial portions of the Software.
 *
 *   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 *   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 *   OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 *   NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 *   HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 *   WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 *   FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 *   OTHER DEALINGS IN THE SOFTWARE.
 *
 * ----------------------------------------------------------------------- */

/*
 * initramfs_file.c
 *
 * Utility functions to add arbitrary files including cpio header
 */

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <syslinux/linux.h>

#define CPIO_MAGIC "070701"
struct cpio_header {
    char c_magic[6];		/* 070701 */
    char c_ino[8];		/* Inode number */
    char c_mode[8];		/* File mode and permissions */
    char c_uid[8];		/* uid */
    char c_gid[8];		/* gid */
    char c_nlink[8];		/* Number of links */
    char c_mtime[8];		/* Modification time */
    char c_filesize[8];		/* Size of data field */
    char c_maj[8];		/* File device major number */
    char c_min[8];		/* File device minor number */
    char c_rmaj[8];		/* Device node reference major number */
    char c_rmin[8];		/* Device node reference minor number */
    char c_namesize[8];		/* Length of filename including final \0 */
    char c_chksum[8];		/* Checksum if c_magic ends in 2 */
};

static uint32_t next_ino = 1;

/* Create cpio headers for the directory entries leading up to a file.
   Returns the number of bytes; doesn't touch the buffer if too small. */
static size_t initramfs_mkdirs(const char *filename, void *buffer,
			       size_t buflen)
{
    const char *p = filename;
    char *bp = buffer;
    int len;
    size_t bytes = 0;
    int pad;

    while ((p = strchr(p, '/'))) {
	if (p != filename && p[-1] != '/') {
	    len = p - filename;
	    bytes += ((sizeof(struct cpio_header) + len + 1) + 3) & ~3;
	}
	p++;
    }

    if (buflen >= bytes) {
	p = filename;
	while ((p = strchr(p, '/'))) {
	    if (p != filename && p[-1] != '/') {
		len = p - filename;
		bp += sprintf(bp, "070701%08x%08x%08x%08x%08x%08x%08x%08x%08x"
			      "%08x%08x%08x%08x", next_ino++, S_IFDIR | 0755,
			      0, 0, 1, 0, 0, 0, 1, 0, 1, len + 1, 0);
		memcpy(bp, filename, len);
		bp += len;
		pad = (-(sizeof(struct cpio_header) + len) & 3) + 1;
		memset(bp, 0, pad);
		bp += pad;
	    }
	}
    }

    return bytes;
}

/*
 * Create a file header (with optional parent directory entries)
 * and add it to an initramfs chain
 */
int initramfs_mknod(struct initramfs *ihead, const char *filename,
		    int do_mkdir,
		    uint16_t mode, size_t len, uint32_t major, uint32_t minor)
{
    size_t bytes;
    int namelen = strlen(filename);
    int pad;
    char *buffer, *bp;

    if (do_mkdir)
	bytes = initramfs_mkdirs(filename, NULL, 0);
    else
	bytes = 0;

    bytes += ((sizeof(struct cpio_header) + namelen + 1) + 3) & ~3;

    bp = buffer = malloc(bytes);
    if (!buffer)
	return -1;

    if (do_mkdir)
	bp += initramfs_mkdirs(filename, bp, bytes);

    bp += sprintf(bp, "070701%08x%08x%08x%08x%08x%08x%08x%08x%08x"
		  "%08x%08x%08x%08x", next_ino++, mode,
		  0, 0, 1, 0, len, 0, 1, major, minor, namelen + 1, 0);
    memcpy(bp, filename, namelen);
    bp += len;
    pad = (-(sizeof(struct cpio_header) + namelen) & 3) + 1;
    memset(bp, 0, pad);

    if (initramfs_add_data(ihead, buffer, bytes, bytes, 4)) {
	free(buffer);
	return -1;
    }

    return 0;
}

/*
 * Add a file given data in memory to an initramfs chain.  This
 * can be used to create nonfiles like symlinks by specifying an
 * appropriate mode.
 */
int initramfs_add_file(struct initramfs *ihead, const void *data,
		       size_t data_len, size_t len,
		       const char *filename, int do_mkdir, uint32_t mode)
{
    if (initramfs_mknod(ihead, filename, do_mkdir,
			(mode & S_IFMT) ? mode : mode | S_IFREG, len, 0, 1))
	return -1;

    return initramfs_add_data(ihead, data, data_len, len, 4);
}

int initramfs_add_trailer(struct initramfs *ihead)
{
    return initramfs_mknod(ihead, "TRAILER!!!", 0, 0, 0, 0, 0);
}