aboutsummaryrefslogtreecommitdiffstats
path: root/com32/modules/linux.c
blob: f657eab4130068ffbf05db3f5d9ca100d9145fde (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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
/* ----------------------------------------------------------------------- *
 *
 *   Copyright 2007-2008 H. Peter Anvin - All Rights Reserved
 *   Copyright 2009-2012 Intel Corporation; author: H. Peter Anvin
 *
 *   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.
 *
 * ----------------------------------------------------------------------- */

/*
 * linux.c
 *
 * Sample module to load Linux kernels.  This module can also create
 * a file out of the DHCP return data if running under PXELINUX.
 *
 * If -dhcpinfo is specified, the DHCP info is written into the file
 * /dhcpinfo.dat in the initramfs.
 *
 * Usage: linux.c32 [-dhcpinfo] kernel arguments...
 */

#include <errno.h>
#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <console.h>
#include <syslinux/loadfile.h>
#include <syslinux/linux.h>
#include <syslinux/pxe.h>

enum ldmode {
    ldmode_raw,
    ldmode_cpio,
    ldmodes
};

typedef int f_ldinitramfs(struct initramfs *, char *);

const char *progname = "linux.c32";

/* Find the last instance of a particular command line argument
   (which should include the final =; do not use for boolean arguments) */
static char *find_argument(char **argv, const char *argument)
{
    int la = strlen(argument);
    char **arg;
    char *ptr = NULL;

    for (arg = argv; *arg; arg++) {
	if (!strncmp(*arg, argument, la))
	    ptr = *arg + la;
    }

    return ptr;
}

/* Find the next instance of a particular command line argument */
static char **find_arguments(char **argv, char **ptr,
			     const char *argument)
{
    int la = strlen(argument);
    char **arg;

    for (arg = argv; *arg; arg++) {
	if (!strncmp(*arg, argument, la)) {
	    *ptr = *arg + la;
	    break;
	}
    }

    /* Exhausted all arguments */
    if (!*arg)
	return NULL;

    return arg;
}

/* Search for a boolean argument; return its position, or 0 if not present */
static int find_boolean(char **argv, const char *argument)
{
    char **arg;

    for (arg = argv; *arg; arg++) {
	if (!strcmp(*arg, argument))
	    return (arg - argv) + 1;
    }

    return 0;
}

/* Stitch together the command line from a set of argv's */
static char *make_cmdline(char **argv)
{
    char **arg;
    size_t bytes;
    char *cmdline, *p;

    bytes = 1;			/* Just in case we have a zero-entry cmdline */
    for (arg = argv; *arg; arg++) {
	bytes += strlen(*arg) + 1;
    }

    p = cmdline = malloc(bytes);
    if (!cmdline)
	return NULL;

    for (arg = argv; *arg; arg++) {
	int len = strlen(*arg);
	memcpy(p, *arg, len);
	p[len] = ' ';
	p += len + 1;
    }

    if (p > cmdline)
	p--;			/* Remove the last space */
    *p = '\0';

    return cmdline;
}

static f_ldinitramfs ldinitramfs_raw;
static int ldinitramfs_raw(struct initramfs *initramfs, char *fname)
{
    return initramfs_load_archive(initramfs, fname);
}

static f_ldinitramfs ldinitramfs_cpio;
static int ldinitramfs_cpio(struct initramfs *initramfs, char *fname)
{
    char *target_fname, *p;
    int do_mkdir, unmangle, rc;

    /* Choose target_fname based on presence of "@" syntax */
    target_fname = strchr(fname, '@');
    if (target_fname) {
	/* Temporarily mangle */
	unmangle = 1;
	*target_fname++ = '\0';

	/* Make parent directories? */
	do_mkdir = !!strchr(target_fname, '/');
    } else {
	unmangle = 0;

	/* Forget the source path */
	target_fname = fname;
	while ((p = strchr(target_fname, '/')))
	    target_fname = p + 1;

	/* The user didn't specify a desired path */
	do_mkdir = 0;
    }

    /*
     * Load the file, encapsulate it with the desired path, make the
     * parent directories if the desired path contains them, add to initramfs
     */
    rc = initramfs_load_file(initramfs, fname, target_fname, do_mkdir, 0755);

    /* Unmangle, if needed*/
    if (unmangle)
	*--target_fname = '@';

    return rc;
}

/* It only makes sense to call this function from main */
static int process_initramfs_args(char *arg, struct initramfs *initramfs,
				  const char *kernel_name, enum ldmode mode,
				  bool opt_quiet)
{
    const char *mode_msg;
    f_ldinitramfs *ldinitramfs;
    char *p;

    switch (mode) {
    case ldmode_raw:
	mode_msg = "Loading";
	ldinitramfs = ldinitramfs_raw;
	break;
    case ldmode_cpio:
	mode_msg = "Encapsulating";
	ldinitramfs = ldinitramfs_cpio;
	break;
    case ldmodes:
    default:
	return 1;
    }

    do {
	p = strchr(arg, ',');
	if (p)
	    *p = '\0';

	if (!opt_quiet)
	    printf("%s %s... ", mode_msg, arg);
	errno = 0;
	if (ldinitramfs(initramfs, arg)) {
	    if (opt_quiet)
		printf("Loading %s ", kernel_name);
	    printf("failed: ");
	    return 1;
	}
	if (!opt_quiet)
	    printf("ok\n");

	if (p)
	    *p++ = ',';
    } while ((arg = p));

    return 0;
}

static int setup_data_file(struct setup_data *setup_data,
			   uint32_t type, const char *filename,
			   bool opt_quiet)
{
    if (!opt_quiet)
	printf("Loading %s... ", filename);

    if (setup_data_load(setup_data, type, filename)) {
	if (opt_quiet)
	    printf("Loading %s ", filename);
	printf("failed\n");
	return -1;
    }
	    
    if (!opt_quiet)
	printf("ok\n");
    
    return 0;
}

int main(int argc, char *argv[])
{
    const char *kernel_name;
    struct initramfs *initramfs;
    struct setup_data *setup_data;
    char *cmdline;
    char *boot_image;
    void *kernel_data;
    size_t kernel_len;
    bool opt_dhcpinfo = false;
    bool opt_quiet = false;
    void *dhcpdata;
    size_t dhcplen;
    char **argp, **argl, *arg;

    (void)argc;
    argp = argv + 1;

    while ((arg = *argp) && arg[0] == '-') {
	if (!strcmp("-dhcpinfo", arg)) {
	    opt_dhcpinfo = true;
	} else {
	    fprintf(stderr, "%s: unknown option: %s\n", progname, arg);
	    return 1;
	}
	argp++;
    }

    if (!arg) {
	fprintf(stderr, "%s: missing kernel name\n", progname);
	return 1;
    }

    kernel_name = arg;

    errno = 0;
    boot_image = malloc(strlen(kernel_name) + 12);
    if (!boot_image) {
	fprintf(stderr, "Error allocating BOOT_IMAGE string: ");
	goto bail;
    }
    strcpy(boot_image, "BOOT_IMAGE=");
    strcpy(boot_image + 11, kernel_name);
    /* argp now points to the kernel name, and the command line follows.
       Overwrite the kernel name with the BOOT_IMAGE= argument, and thus
       we have the final argument. */
    *argp = boot_image;

    if (find_boolean(argp, "quiet"))
	opt_quiet = true;

    if (!opt_quiet)
	printf("Loading %s... ", kernel_name);
    errno = 0;
    if (loadfile(kernel_name, &kernel_data, &kernel_len)) {
	if (opt_quiet)
	    printf("Loading %s ", kernel_name);
	printf("failed: ");
	goto bail;
    }
    if (!opt_quiet)
	printf("ok\n");

    errno = 0;
    cmdline = make_cmdline(argp);
    if (!cmdline) {
	fprintf(stderr, "make_cmdline() failed: ");
	goto bail;
    }

    /* Initialize the initramfs chain */
    errno = 0;
    initramfs = initramfs_init();
    if (!initramfs) {
	fprintf(stderr, "initramfs_init() failed: ");
	goto bail;
    }

    /* Process initramfs arguments */
    if ((arg = find_argument(argp, "initrd="))) {
	if (process_initramfs_args(arg, initramfs, kernel_name, ldmode_raw,
				   opt_quiet))
	    goto bail;
    }

    argl = argv;
    while ((argl = find_arguments(argl, &arg, "initrd+="))) {
	argl++;
	if (process_initramfs_args(arg, initramfs, kernel_name, ldmode_raw,
				   opt_quiet))
	    goto bail;
    }

    argl = argv;
    while ((argl = find_arguments(argl, &arg, "initrdfile="))) {
	argl++;
	if (process_initramfs_args(arg, initramfs, kernel_name, ldmode_cpio,
				   opt_quiet))
	    goto bail;
    }

    /* Append the DHCP info */
    if (opt_dhcpinfo &&
	!pxe_get_cached_info(PXENV_PACKET_TYPE_DHCP_ACK, &dhcpdata, &dhcplen)) {
	errno = 0;
	if (initramfs_add_file(initramfs, dhcpdata, dhcplen, dhcplen,
			       "/dhcpinfo.dat", 0, 0755)) {
	    fprintf(stderr, "Unable to add DHCP info: ");
	    goto bail;
	}
    }

    /* Handle dtb and eventually other setup data */
    setup_data = setup_data_init();
    if (!setup_data)
	goto bail;

    argl = argv;
    while ((argl = find_arguments(argl, &arg, "dtb="))) {
	argl++;
	if (setup_data_file(setup_data, SETUP_DTB, arg, opt_quiet))
	    goto bail;
    }

    argl = argv;
    while ((argl = find_arguments(argl, &arg, "blob."))) {
	uint32_t type;
	char *ep;

	argl++;
	type = strtoul(arg, &ep, 10);
	if (ep[0] != '=' || !ep[1])
	    continue;

	if (!type)
	    continue;

	if (setup_data_file(setup_data, type, ep+1, opt_quiet))
	    goto bail;
    }

    /* This should not return... */
    errno = 0;
    syslinux_boot_linux(kernel_data, kernel_len, initramfs,
			setup_data, cmdline);
    fprintf(stderr, "syslinux_boot_linux() failed: ");

bail:
    switch(errno) {
    case ENOENT:
	fprintf(stderr, "File not found\n");
	break;
    case ENOMEM:
	fprintf(stderr, "Out of memory\n");
	break;
    default:
	fprintf(stderr, "Error %d\n", errno);
	break;
    }
    fprintf(stderr, "%s: Boot aborted!\n", progname);
    return 1;
}