summaryrefslogtreecommitdiff
path: root/xen/arch/x86/boot/cmdline.c
blob: fc11c6d3c5c45fc8cdcd51ddd12923bfee3c0447 (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
/*
 * Copyright (c) 2015, 2016 Oracle and/or its affiliates. All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * strlen(), strncmp(), strchr(), strspn() and strcspn() were copied from
 * Linux kernel source (linux/lib/string.c).
 */

/*
 * This entry point is entered from xen/arch/x86/boot/head.S with:
 *   - 0x4(%esp) = &cmdline,
 *   - 0x8(%esp) = &early_boot_opts.
 */
asm (
    "    .text                         \n"
    "    .globl _start                 \n"
    "_start:                           \n"
    "    jmp  cmdline_parse_early      \n"
    );

#include <xen/kconfig.h>
#include "defs.h"
#include "video.h"

/* Keep in sync with trampoline.S:early_boot_opts label! */
typedef struct __packed {
    u8 skip_realmode;
    u8 opt_edd;
    u8 opt_edid;
    u8 padding;
#ifdef CONFIG_VIDEO
    u16 boot_vid_mode;
    u16 vesa_width;
    u16 vesa_height;
    u16 vesa_depth;
#endif
} early_boot_opts_t;

/*
 * Space and TAB are obvious delimiters. However, I am
 * adding "\n" and "\r" here too. Just in case when
 * crazy bootloader/user puts them somewhere.
 */
static const char delim_chars_comma[] = ", \n\r\t";

#define delim_chars	(delim_chars_comma + 1)

static size_t strlen(const char *s)
{
    const char *sc;

    for ( sc = s; *sc != '\0'; ++sc )
        /* nothing */;
    return sc - s;
}

static int strncmp(const char *cs, const char *ct, size_t count)
{
    unsigned char c1, c2;

    while ( count )
    {
        c1 = *cs++;
        c2 = *ct++;
        if ( c1 != c2 )
            return c1 < c2 ? -1 : 1;
        if ( !c1 )
            break;
        count--;
    }
    return 0;
}

static char *strchr(const char *s, int c)
{
    for ( ; *s != (char)c; ++s )
        if ( *s == '\0' )
            return NULL;
    return (char *)s;
}

static size_t strspn(const char *s, const char *accept)
{
    const char *p;
    const char *a;
    size_t count = 0;

    for ( p = s; *p != '\0'; ++p )
    {
        for ( a = accept; *a != '\0'; ++a )
        {
            if ( *p == *a )
                break;
        }
        if ( *a == '\0' )
            return count;
        ++count;
    }
    return count;
}

static size_t strcspn(const char *s, const char *reject)
{
    const char *p;
    const char *r;
    size_t count = 0;

    for ( p = s; *p != '\0'; ++p )
    {
        for ( r = reject; *r != '\0'; ++r )
        {
            if ( *p == *r )
                return count;
        }
        ++count;
    }
    return count;
}

static unsigned int __maybe_unused strtoui(
    const char *s, const char *stop, const char **next)
{
    char base = 10, l;
    unsigned long long res = 0;

    if ( *s == '0' )
      base = (tolower(*++s) == 'x') ? (++s, 16) : 8;

    for ( ; *s != '\0'; ++s )
    {
        if ( stop && strchr(stop, *s) )
            goto out;

        if ( *s < '0' || (*s > '7' && base == 8) )
        {
            res = UINT_MAX;
            goto out;
        }

        l = tolower(*s);

        if ( *s > '9' && (base != 16 || l < 'a' || l > 'f') )
        {
            res = UINT_MAX;
            goto out;
        }

        res *= base;
        res += (l >= 'a') ? (l - 'a' + 10) : (*s - '0');

        if ( res >= UINT_MAX )
        {
            res = UINT_MAX;
            goto out;
        }
    }

 out:
    if ( next )
      *next = s;

    return res;
}

static int strmaxcmp(const char *cs, const char *ct, const char *_delim_chars)
{
    return strncmp(cs, ct, max(strcspn(cs, _delim_chars), strlen(ct)));
}

static int __maybe_unused strsubcmp(const char *cs, const char *ct)
{
    return strncmp(cs, ct, strlen(ct));
}

static const char *find_opt(const char *cmdline, const char *opt, bool arg)
{
    size_t lc, lo;

    lo = strlen(opt);

    for ( ; ; )
    {
        cmdline += strspn(cmdline, delim_chars);

        if ( *cmdline == '\0' )
            return NULL;

        if ( !strmaxcmp(cmdline, "--", delim_chars) )
            return NULL;

        lc = strcspn(cmdline, delim_chars);

        if ( !strncmp(cmdline, opt, arg ? lo : max(lc, lo)) )
            return cmdline + lo;

        cmdline += lc;
    }
}

static bool skip_realmode(const char *cmdline)
{
    return find_opt(cmdline, "no-real-mode", false) || find_opt(cmdline, "tboot=", true);
}

static u8 edd_parse(const char *cmdline)
{
    const char *c;

    c = find_opt(cmdline, "edd=", true);

    if ( !c )
        return 0;

    if ( !strmaxcmp(c, "off", delim_chars) )
        return 2;

    return !strmaxcmp(c, "skipmbr", delim_chars);
}

static u8 edid_parse(const char *cmdline)
{
    const char *c;

    c = find_opt(cmdline, "edid=", true);

    if ( !c )
        return 0;

    if ( !strmaxcmp(c, "force", delim_chars) )
        return 2;

    return !strmaxcmp(c, "no", delim_chars);
}

#ifdef CONFIG_VIDEO
static u16 rows2vmode(unsigned int rows)
{
    switch ( rows )
    {
    case 25:
        return VIDEO_80x25;

    case 28:
        return VIDEO_80x28;

    case 30:
        return VIDEO_80x30;

    case 34:
        return VIDEO_80x34;

    case 43:
        return VIDEO_80x43;

    case 50:
        return VIDEO_80x50;

    case 60:
        return VIDEO_80x60;

    default:
        return ASK_VGA;
    }
}

static void vga_parse(const char *cmdline, early_boot_opts_t *ebo)
{
    const char *c;
    unsigned int tmp, vesa_depth, vesa_height, vesa_width;

    c = find_opt(cmdline, "vga=", true);

    if ( !c )
        return;

    ebo->boot_vid_mode = ASK_VGA;

    if ( !strmaxcmp(c, "current", delim_chars_comma) )
        ebo->boot_vid_mode = VIDEO_CURRENT_MODE;
    else if ( !strsubcmp(c, "text-80x") )
    {
        c += strlen("text-80x");
        ebo->boot_vid_mode = rows2vmode(strtoui(c, delim_chars_comma, NULL));
    }
    else if ( !strsubcmp(c, "gfx-") )
    {
        vesa_width = strtoui(c + strlen("gfx-"), "x", &c);

        if ( vesa_width > U16_MAX )
            return;

        /*
         * Increment c outside of strtoui() because otherwise some
         * compiler may complain with following message:
         * warning: operation on 'c' may be undefined.
         */
        ++c;
        vesa_height = strtoui(c, "x", &c);

        if ( vesa_height > U16_MAX )
            return;

        vesa_depth = strtoui(++c, delim_chars_comma, NULL);

        if ( vesa_depth > U16_MAX )
            return;

        ebo->vesa_width = vesa_width;
        ebo->vesa_height = vesa_height;
        ebo->vesa_depth = vesa_depth;
        ebo->boot_vid_mode = VIDEO_VESA_BY_SIZE;
    }
    else if ( !strsubcmp(c, "mode-") )
    {
        tmp = strtoui(c + strlen("mode-"), delim_chars_comma, NULL);

        if ( tmp > U16_MAX )
            return;

        ebo->boot_vid_mode = tmp;
    }
}
#endif

void __stdcall cmdline_parse_early(const char *cmdline, early_boot_opts_t *ebo)
{
    if ( !cmdline )
        return;

    ebo->skip_realmode = skip_realmode(cmdline);
    ebo->opt_edd = edd_parse(cmdline);
    ebo->opt_edid = edid_parse(cmdline);

#ifdef CONFIG_VIDEO
    vga_parse(cmdline, ebo);
#endif
}