2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Portions Copyright (c) 1999 Apple Computer, Inc. All Rights
7 * Reserved. This file contains Original Code and/or Modifications of
8 * Original Code as defined in and that are subject to the Apple Public
9 * Source License Version 1.1 (the "License"). You may not use this file
10 * except in compliance with the License. Please obtain a copy of the
11 * License at http://www.apple.com/publicsource and read it before using
14 * The Original Code and all software distributed under the License are
15 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE OR NON- INFRINGEMENT. Please see the
19 * License for the specific language governing rights and limitations
22 * @APPLE_LICENSE_HEADER_END@
25 * Copyright 1993 NeXT, Inc.
26 * All rights reserved.
30 #import "bootstruct.h"
31 #import <driverkit/configTablePrivate.h>
33 extern char *Language
;
34 extern char *LoadableFamilies
;
36 static void eatThru(char val
, char **table_p
);
38 static inline int isspace(char c
)
40 return (c
== ' ' || c
== '\t');
44 * Compare a string to a key with quoted characters
47 keyncmp(char *str
, char *key
, int n
)
66 } else if (c
== '\"') {
67 /* Premature end of key */
77 static void eatThru(char val
, char **table_p
)
79 register char *table
= *table_p
;
80 register BOOL found
= NO
;
82 while (*table
&& !found
)
84 if (*table
== '\\') table
+= 2;
87 if (*table
== val
) found
= YES
;
100 char *begin
= *list
, *end
;
104 while (*begin
&& newsize
&& isspace(*begin
)) {
109 while (*end
&& newsize
&& !isspace(*end
)) {
115 newstr
= malloc(end
- begin
+ 1);
116 strncpy(newstr
, begin
, end
- begin
);
123 * compress == compress escaped characters to one character
125 int stringLength(char *table
, int compress
)
134 ret
+= 1 + (compress
? 0 : 1);
138 if (*table
== '\"') return ret
;
147 // looks in table for strings of format << "key" = "value"; >>
149 BOOL
getValueForStringTableKey(char *table
, char *key
, char **val
, int *size
)
156 eatThru('\"',&table
);
158 keyLength
= strlen(key
);
160 (stringLength(table
,1) == keyLength
) &&
161 (keyncmp(key
, table
, keyLength
) == 0))
165 /* found the key; now look for either
173 } else if (c
== '=' || c
== ';') {
180 eatThru('\"',&table
);
183 *size
= stringLength(table
,0);
196 * Returns a new malloc'ed string if one is found
197 * in the string table matching 'key'. Also translates
198 * \n escapes in the string.
200 char *newStringForStringTableKey(
205 char *val
, *newstr
, *p
;
208 if (getValueForStringTableKey(table
, key
, &val
, &size
)) {
209 newstr
= malloc(size
+1);
210 for (p
= newstr
; size
; size
--, p
++, val
++) {
211 if ((*p
= *val
) == '\\') {
237 newStringForKey(char *key
)
242 if (getValueForKey(key
, &val
, &size
) && size
) {
243 newstr
= malloc(size
+ 1);
244 strncpy(newstr
, val
, size
);
251 /* parse a command line
252 * in the form: [<argument> ...] [<option>=<value> ...]
253 * both <option> and <value> must be either composed of
254 * non-whitespace characters, or enclosed in quotes.
257 static char *getToken(char *line
, char **begin
, int *len
)
261 while (*line
&& *line
!= '\"')
263 *len
= line
++ - *begin
;
266 while (*line
&& !isspace(*line
) && *line
!= '=')
268 *len
= line
- *begin
;
273 BOOL
getValueForBootKey(char *line
, char *match
, char **matchval
, int *len
)
276 int key_len
, value_len
;
279 /* look for keyword or argument */
280 while (isspace(*line
)) line
++;
282 /* now look for '=' or whitespace */
283 line
= getToken(line
, &key
, &key_len
);
284 /* line now points to '=' or space */
285 if (!isspace(*line
++)) {
286 line
= getToken(line
, &value
, &value_len
);
287 if ((strlen(match
) == key_len
)
288 && strncmp(match
, key
, key_len
) == 0) {
305 if (getValueForKey(key
, &val
, &size
) && (size
>= 1) &&
306 val
[0] == 'Y' || val
[0] == 'y')
317 if (getValueForBootKey(bootArgs
->bootString
, key
, val
, size
))
319 else if (getValueForStringTableKey(bootArgs
->config
, key
, val
, size
))
325 #define LOCALIZABLE \
326 "/usr/Devices/%s.config/%s.lproj/Localizable.strings"
329 loadLocalizableStrings(
333 char buf
[256], *config
;
334 register int count
, fd
;
336 sprintf(buf
, LOCALIZABLE
, name
, Language
);
337 if ((fd
= open(buf
,0)) < 0) {
338 sprintf(buf
, LOCALIZABLE
, name
, "English");
339 if ((fd
= open(buf
,0)) < 0)
342 count
= file_size(fd
);
343 config
= malloc(count
);
344 count
= read(fd
, config
, count
);
358 char *table
, *name
, *val
;
361 if ((table
= loadLocalizableStrings(bundleName
)) != 0 &&
362 getValueForStringTableKey(table
,"Long Name", &val
, &size
) == YES
) {
363 name
= malloc(size
+1);
364 strncpy(name
, val
, size
);
367 name
= newString(bundleName
);
376 * Returns 0 if file loaded OK,
377 * -1 if file was not loaded
378 * Does not print error messages.
379 * Returns pointer to table in memory in *table.
382 loadConfigFile( char *configFile
, char **table
, int allocTable
)
384 char *configPtr
= bootArgs
->configEnd
;
387 /* Read config file into memory */
388 if ((fd
= open(configFile
, 0)) >= 0)
391 configPtr
= malloc(file_size(fd
)+2);
393 if ((configPtr
- bootArgs
->config
) > CONFIG_SIZE
) {
394 error("No room in memory for config file %s\n",configFile
);
398 localPrintf("Reading config: %s\n",configFile
);
400 if (table
) *table
= configPtr
;
401 count
= read(fd
, configPtr
, IO_CONFIG_DATA_SIZE
);
408 bootArgs
->configEnd
= configPtr
;
416 /* Returns 0 if requested config files were loaded,
417 * 1 if default files were loaded,
418 * -1 if no files were loaded.
419 * Prints error message if files cannot be loaded.
424 char *bundleName
, // bundle directory name (e.g. "System.config")
425 int useDefault
, // use Default.table instead of instance tables
426 char **table
, // returns pointer to config table
427 int allocTable
// malloc the table and return in *table
436 // load up to 99 instance tables
437 for (i
=0; i
< 99; i
++) {
438 sprintf(buf
, "%s%s.config/Instance%d.table", IO_CONFIG_DIR
,
440 if (useDefault
|| (loadConfigFile(buf
, table
, allocTable
) != 0)) {
442 // couldn't load first instance table;
443 // try the default table
444 sprintf(buf
, "%s%s.config/%s", IO_CONFIG_DIR
, bundleName
,
445 IO_DEFAULT_TABLE_FILENAME
);
446 if (loadConfigFile(buf
, table
, allocTable
) == 0) {
450 error("Config file \"%s\" not found\n", buf
);
463 #define SYSTEM_DEFAULT_FILE \
464 IO_SYSTEM_CONFIG_DIR IO_DEFAULT_TABLE_FILENAME
465 #define SYSTEM_CONFIG "System"
469 /* Returns 0 if requested config files were loaded,
470 * 1 if default files were loaded,
471 * -1 if no files were loaded.
472 * Prints error message if files cannot be loaded.
481 int ret
, len
, doDefault
=0;
483 buf
= bp
= malloc(256);
486 for(cp
= which
, len
= size
; len
&& *cp
&& *cp
!= LP
; cp
++, len
--) ;
488 while (len
-- && *cp
&& *cp
++ != RP
) ;
489 /* cp now points past device */
490 strncpy(buf
,which
,cp
- which
);
497 strcpy(bp
, IO_SYSTEM_CONFIG_DIR
);
498 strncat(bp
, cp
, len
);
499 if (strncmp(cp
+ len
- strlen(IO_TABLE_EXTENSION
),
500 IO_TABLE_EXTENSION
, strlen(IO_TABLE_EXTENSION
)) != 0)
501 strcat(bp
, IO_TABLE_EXTENSION
);
503 strncpy(bp
, cp
, len
);
506 if (strcmp(bp
, SYSTEM_DEFAULT_FILE
) == 0)
508 ret
= loadConfigFile(bp
= buf
, 0, 0);
510 ret
= loadConfigDir((bp
= SYSTEM_CONFIG
), 0, 0, 0);
513 error("System config file '%s' not found\n", bp
);
517 return (ret
< 0 ? ret
: doDefault
);
531 if (getValueForKey( "Boot Drivers", &val
, &count
))
533 while (string
= newStringFromList(&val
, &count
)) {
534 ret
= loadConfigDir(string
, useDefault
, &table
, 0);
536 if ((fd
= openDriverReloc(string
)) >= 0) {
537 localPrintf("Loading binary for %s\n",string
);
538 if (loadDriver(string
, fd
) < 0)
539 error("Error loading driver %s\n",string
);
542 driverWasLoaded(string
, table
);
545 driverIsMissing(string
);
548 useDefault
= 1; // use defaults from now on
551 error("Warning: No active drivers specified in system config\n");
554 bootArgs
->first_addr0
=
555 (int)bootArgs
->configEnd
+ 1024;