aboutsummaryrefslogtreecommitdiffstats
path: root/com32/mboot/mboot.c
blob: 16579e434782213b20ab6f9f5cb04e9f4c7f025f (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
/* ----------------------------------------------------------------------- *
 *
 *   Copyright 2007-2008 H. Peter Anvin - All Rights Reserved
 *   Copyright 2009-2010 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.
 *
 * ----------------------------------------------------------------------- */

/*
 * mboot.c
 *
 * Module to load a multiboot kernel
 */

#include "mboot.h"

struct multiboot_info mbinfo;
struct syslinux_pm_regs regs;
struct my_options opt;

struct module_data {
    void *data;
    size_t len;
    const char *cmdline;
};

static int map_modules(struct module_data *modules, int nmodules)
{
    struct mod_list *mod_list;
    addr_t map_list = 0;
    size_t list_size = nmodules * sizeof *mod_list;
    int i;

    mod_list = malloc(list_size);
    if (!mod_list) {
	printf("Failed to allocate module list\n");
	return -1;
    }

    map_list = map_data(mod_list, list_size, 16, 0);
    if (!map_list) {
	printf("Cannot map module list\n");
	return -1;
    }

    for (i = 0; i < nmodules; i++) {
	addr_t mod_map = 0;
	addr_t cmd_map = 0;

	dprintf("Module %d cmdline: \"%s\"\n", i, modules[i].cmdline);

	cmd_map = map_string(modules[i].cmdline);

	mod_map = map_data(modules[i].data, modules[i].len, 4096, MAP_HIGH);
	if (!mod_map) {
	    printf("Failed to map module (memory fragmentation issue?)\n");
	    return -1;
	}
	mod_list[i].mod_start = mod_map;
	mod_list[i].mod_end = mod_map + modules[i].len;
	mod_list[i].cmdline = cmd_map;
	mod_list[i].pad = 0;
    }

    mbinfo.flags |= MB_INFO_MODS;
    mbinfo.mods_count = nmodules;
    mbinfo.mods_addr = map_list;
    return 0;
}

static int get_modules(char **argv, struct module_data **mdp)
{
    char **argp, **argx;
    struct module_data *mp;
    int rv;
    int module_count = 1;
    int arglen;
    const char module_separator[] = "---";

    for (argp = argv; *argp; argp++) {
	if (!strcmp(*argp, module_separator))
	    module_count++;
    }

    *mdp = mp = malloc(module_count * sizeof(struct module_data));
    if (!mp) {
	error("Out of memory!\n");
	return -1;
    }

    argp = argv;
    while (*argp) {
	/* Note: it seems Grub transparently decompresses all compressed files,
	   not just the primary kernel. */
	printf("Loading %s... ", *argp);
	rv = zloadfile(*argp, &mp->data, &mp->len);

	if (rv) {
	    printf("failed!\n");
	    return -1;
	}
	printf("ok\n");

	/* 
	 * Note: Grub includes the kernel filename in the command line, so we
	 * want to match that behavior.
	 */
	arglen = 0;
	for (argx = argp; *argx && strcmp(*argx, module_separator); argx++)
	    arglen += strlen(*argx) + 1;

	if (arglen == 0) {
	    mp->cmdline = strdup("");
	} else {
	    char *p;
	    mp->cmdline = p = malloc(arglen);
	    for (; *argp && strcmp(*argp, module_separator); argp++) {
		p = stpcpy(p, *argp);
		*p++ = ' ';
	    }
	    *--p = '\0';
	}
	mp++;
	if (*argp)
	    argp++;		/* Advance past module_separator */
    }

    return module_count;
}

int main(int argc, char *argv[])
{
    int nmodules;
    struct module_data *modules;
    struct multiboot_header *mbh;
    bool keeppxe = false;

    openconsole(&dev_null_r, &dev_stdcon_w);

    (void)argc;			/* Unused */
    argv++;

    while (*argv) {
	if (!strcmp(*argv, "-solaris"))
	    opt.solaris = true;
	else if (!strcmp(*argv, "-aout"))
	    opt.aout = true;
	else
	    break;
	argv++;
    }

    if (!*argv) {
	error
	    ("Usage: mboot.c32 [opts] mboot_file args... [--- module args...]...\n"
	     "Options:\n"
	     "  -solaris  Enable Solaris DHCP information passing\n"
	     "  -aout     Use the \"a.out kludge\" if enabled, even for ELF\n"
	     "            This matches the Multiboot spec, but differs from Grub\n");
	return 1;
    }

    /* Load the files */
    nmodules = get_modules(argv, &modules);
    if (nmodules < 1) {
	error("No files found!\n");
	return 1;		/* Failure */
    }

    if (init_map())
	return 1;		/* Failed to allocate intitial map */

    /*
     * Map the primary image.  This should be done before mapping anything
     * else, since it will have fixed address requirements.
     */
    mbh = map_image(modules[0].data, modules[0].len);
    if (!mbh)
	return 1;

    /* Map the mbinfo structure */
    regs.ebx = map_data(&mbinfo, sizeof mbinfo, 4, 0);
    if (!regs.ebx) {
	error("Failed to map Multiboot info structure!\n");
	return 1;
    }

    /* Map the primary command line */
    if (modules[0].cmdline) {
	mbinfo.cmdline = map_string(modules[0].cmdline);
	dprintf("Main cmdline: \"%s\"\n", modules[0].cmdline);
	if (mbinfo.cmdline)
	    mbinfo.flags |= MB_INFO_CMDLINE;
    }

    /* Map auxilliary images */
    if (nmodules > 1) {
	if (map_modules(modules + 1, nmodules - 1))
	    return 1;
    }

    /* Add auxilliary information */
    mboot_make_memmap();
    mboot_apm();
    mboot_syslinux_info();

    if (opt.solaris)
	mboot_solaris_dhcp_hack();

    /* Set the graphics mode if requested */
    set_graphics_mode(mbh, &mbinfo);

    /* Run it */
    mboot_run(keeppxe ? 3 : 0);
    error("mboot.c32: boot failed\n");
    return 1;
}