summaryrefslogtreecommitdiffstats
path: root/mdk-stage1/mkinitrd_helper/mkinitrd_helper.c
blob: 67be7c340a514707f2c649827b290e12ea79691e (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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
/*
 * Guillaume Cottenceau (gc@mandrakesoft.com)
 *
 * Copyright 2001 MandrakeSoft
 *
 * This software is covered by the GPL license.
 *
 * This software may be freely redistributed under the terms of the GNU
 * public license.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 *
 * This little program replaces usual sash and insmod.static based script
 * from mkinitrd (that insmod modules, plus possibly mount a partition and
 * losetup a loopback-based / on the partition).
 *
 *
 * On my machine:
 *   gzipped sash + insmod.static         502491 bytes
 *   gzipped <this-program>                14243 bytes
 *
 * There will be room for linux-2.4 and many modules, now. Cool.
 *
 */

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <sys/mount.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <signal.h>

#include "insmod.h"

int quiet = 0;

void vlog_message(const char * s, va_list args)
{
	vprintf(s, args);
	printf("\n");
}

void log_perror(char *msg)
{
	perror(msg);
}


static void fatal_error(char *msg)
{
	printf("[] E: %s\n[] giving hand to kernel.\n", msg);
        exit(-1);
}

static void warning(char *msg)
{
	printf("[] W: %s\n", msg);
}

static void parse_parms(const char * parm, char ** parm1, char ** parm2, char ** parm3)
{
	char * ptr;
	
	ptr = strchr(parm, '\n');
	if (!ptr)
		fatal_error("bad config file: no newline after parms");

	*parm1 = malloc(ptr-parm+1); /* yup, never freed :-) */
	memcpy(*parm1, parm, ptr-parm);
	(*parm1)[ptr-parm] = '\0';

	if (!parm2)
		return;

	*parm2 = strchr(*parm1, ' ');
	if (!*parm2)
		return;
	**parm2 = '\0';
	(*parm2)++;

	if (!parm3)
		return;

	*parm3 = strchr(*parm2, ' ');
	if (!*parm3)
		return;
	**parm3 = '\0';
	(*parm3)++;
}


static void insmod_(const char * parm)
{
	char * mod_name, * options;

	parse_parms(parm, &mod_name, &options, NULL);

#ifdef DEBUG
	printf("insmod %s options %s\n", mod_name, options);
#endif
	if (!quiet)
		printf("[] Loading module %s\n", mod_name);

	if (insmod_call(mod_name, options))
		perror("insmod failed");
}


static void mount_(const char * parm)
{
	char * dev, * location, * fs;
	unsigned long flags;
	char * opts = NULL;

	parse_parms(parm, &dev, &location, &fs);

#ifdef DEBUG
	printf("mounting %s on %s as type %s\n", dev, location, fs);
#endif
	if (!quiet)
		printf("[] Mounting device containing loopback root filesystem\n");

	flags = MS_MGC_VAL;

	if (!strcmp(fs, "vfat"))
		opts = "check=relaxed";

	if (mount(dev, location, fs, flags, opts))
		perror("mount failed");
}


#define LO_NAME_SIZE	64
#define LO_KEY_SIZE	32

struct loop_info
{
	int		lo_number;	/* ioctl r/o */
	dev_t		lo_device; 	/* ioctl r/o */
	unsigned long	lo_inode; 	/* ioctl r/o */
	dev_t		lo_rdevice; 	/* ioctl r/o */
	int		lo_offset;
	int		lo_encrypt_type;
	int		lo_encrypt_key_size; 	/* ioctl w/o */
	int		lo_flags;	/* ioctl r/o */
	char		lo_name[LO_NAME_SIZE];
	unsigned char	lo_encrypt_key[LO_KEY_SIZE]; /* ioctl w/o */
	unsigned long	lo_init[2];
	char		reserved[4];
};

#define LOOP_SET_FD	0x4C00
#define LOOP_CLR_FD	0x4C01
#define LOOP_SET_STATUS	0x4C02

static void set_loop_(const char * parm)
{
	struct loop_info loopinfo;
	int fd, ffd;
	char * device, * file;

	parse_parms(parm, &device, &file, NULL);

#ifdef DEBUG
	printf("set_looping %s with %s\n", device, file);
#endif
	if (!quiet)
		printf("[] Setting up loopback file %s\n", file);

	if ((ffd = open(file, O_RDWR)) < 0) {
		perror("set_loop, opening file in rw");
		exit(-1);
	}
  
	if ((fd = open(device, O_RDWR)) < 0) {
		perror("set_loop, opening loop device in rw");
		close(ffd);
		exit(-1);
	}

	memset(&loopinfo, 0, sizeof (loopinfo));
	strncpy(loopinfo.lo_name, file, LO_NAME_SIZE);
	loopinfo.lo_name[LO_NAME_SIZE - 1] = 0;
	loopinfo.lo_offset = 0;

	if (ioctl(fd, LOOP_SET_FD, ffd) < 0) {
		close(fd);
		close(ffd);
		perror("LOOP_SET_FD");
		exit(-1);
	}

	if (ioctl(fd, LOOP_SET_STATUS, &loopinfo) < 0) {
		(void) ioctl (fd, LOOP_CLR_FD, 0);
		close(fd);
		close(ffd);
		perror("LOOP_SET_STATUS");
		exit(-1);
	}

	close(fd);
	close(ffd);
}


#define MD_MAJOR 9
#define RAID_AUTORUN           _IO (MD_MAJOR, 0x14)
#include <linux/raid/md_u.h>

static void raidautorun_(const char * parm)
{
	char * device;
	int fd;

	parse_parms(parm, &device, NULL, NULL);

	if (!quiet)
		printf("[] Calling raid autorun for %s\n", device);
	
	fd = open(device, O_RDWR, 0);
	if (fd < 0) {
		printf("raidautorun: failed to open %s: %d\n", device, errno);
		return;
	}
	
	if (ioctl(fd, RAID_AUTORUN, 0)) {
		printf("raidautorun: RAID_AUTORUN failed: %d\n", errno);
	}
	
	close(fd);
}

static int handle_command(char ** ptr, char * cmd_name, void (*cmd_func)(const char * parm))
{
	if (!strncmp(*ptr, cmd_name, strlen(cmd_name))) {
		*ptr = strchr(*ptr, '\n');
		if (!*ptr)
			fatal_error("Bad config file: no newline after command");
		(*ptr)++;
		cmd_func(*ptr);
		*ptr = strchr(*ptr, '\n');
		if (!*ptr)
			exit(0);
		(*ptr)++;
		return 1;
	}
	return 0;
}


int main(int argc, char **argv)
{
	int fd_conf, i;
	char buf[5000];
	char * ptr;

	if (strstr(argv[0], "modprobe"))
		exit(0);

	if (mount("/proc", "/loopfs", "proc", 0, NULL))
		printf("[] couldn't mount proc filesystem\n");
	else {
		int fd_cmdline = open("/loopfs/cmdline", O_RDONLY);
		if (fd_cmdline > 0) {
			i = read(fd_cmdline, buf, sizeof(buf));
			if (i == -1)
				warning("could not read cmdline");
			else {
				buf[i] = '\0';
				if (strstr(buf, "quiet"))
					quiet = 1;
			}
			close(fd_cmdline);
		}
		umount("/loopfs");
	}

	if (!quiet)
		printf("[] initrd_helper v" VERSION "\n");

	if ((fd_conf = open("/mkinitrd_helper.conf", O_RDONLY)) < 0)
		fatal_error("could not open mkinitrd_helper config file");
	
	i = read(fd_conf, buf, sizeof(buf));
	if (i == -1)
		fatal_error("could not read mkinitrd_helper config file");
	buf[i] = '\0';
	close(fd_conf);

	ptr = buf;

	while (*ptr)
		if (!(handle_command(&ptr, "insmod", insmod_) +
		      handle_command(&ptr, "mount", mount_) +
		      handle_command(&ptr, "raidautorun", raidautorun_) +
		      handle_command(&ptr, "set_loop", set_loop_)))
			warning("unkown command (trying to continue)");

	return 0;
}