1/*
2 * Copyright (c) 2005-2007 Apple Inc. All Rights Reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23/*
24 *  BLPreserveBootArgs.c
25 *  bless
26 *
27 *  Created by Shantonu Sen on 11/16/05.
28 *  Copyright 2005-2007 Apple Inc. All Rights Reserved.
29 *
30 */
31
32#include <stdlib.h>
33#include <stdarg.h>
34#include <stdio.h>
35
36#include "bless.h"
37#include "bless_private.h"
38
39static const char *remove_boot_args[] = {
40	"rd",	/* rd=(enet|disk0|md0) */
41	"rp",	/* rp=nfs:1.2.3.4:/foo:bar.dmg, netboot */
42	"boot-uuid",	/* UUID of filesystem/partition for rooting */
43	NULL
44};
45
46int BLPreserveBootArgs(BLContextPtr context,
47                       const char *input,
48                       char *output,
49                       int outputLen)
50{
51    char oldbootargs[1024];
52    char bootargs[1024];
53    size_t bootleft=sizeof(bootargs)-1;
54    char *token, *restargs;
55    int firstarg=1;
56
57
58    strncpy(oldbootargs, input, sizeof(oldbootargs)-1);
59    oldbootargs[sizeof(oldbootargs)-1] = '\0';
60
61    memset(bootargs, 0, sizeof(bootargs));
62
63    contextprintf(context, kBLLogLevelVerbose,  "Old boot-args: %s\n", oldbootargs);
64
65    restargs = oldbootargs;
66    while((token = strsep(&restargs, " ")) != NULL) {
67        int shouldbesaved = 1, i;
68        contextprintf(context, kBLLogLevelVerbose, "\tGot token: %s\n", token);
69        for(i=0; remove_boot_args[i]; i++) {
70            // see if it's something we want
71            if(remove_boot_args[i][0] == '-') {
72                // -v style
73                if(strcmp(remove_boot_args[i], token) == 0) {
74                    shouldbesaved = 0;
75                    break;
76                }
77            } else {
78                // debug= style
79                int keylen = strlen(remove_boot_args[i]);
80                if(strlen(token) >= keylen+1
81                   && strncmp(remove_boot_args[i], token, keylen) == 0
82                   && token[keylen] == '=') {
83                    shouldbesaved = 0;
84                    break;
85                }
86            }
87        }
88
89        if(shouldbesaved) {
90            // append to bootargs if it should be preserved
91            if(firstarg) {
92                firstarg = 0;
93            } else {
94                strncat(bootargs, " ", bootleft);
95                bootleft--;
96            }
97
98            contextprintf(context, kBLLogLevelVerbose,  "\tPreserving: %s\n", token);
99            strncat(bootargs, token, bootleft);
100            bootleft -= strlen(token);
101        }
102    }
103
104    strlcpy(output, bootargs, outputLen);
105
106    return 0;
107}
108