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 #include "kernBootStruct.h"
31 #include "stringConstants.h"
32 #include "legacy/configTablePrivate.h"
34 extern KERNBOOTSTRUCT
*kernBootStruct
;
35 extern char *Language
;
36 extern char *LoadableFamilies
;
38 static void eatThru(char val
, char **table_p
);
40 static inline int isspace(char c
)
42 return (c
== ' ' || c
== '\t');
46 * Compare a string to a key with quoted characters
49 keyncmp(char *str
, char *key
, int n
)
68 } else if (c
== '\"') {
69 /* Premature end of key */
79 static void eatThru(char val
, char **table_p
)
81 register char *table
= *table_p
;
82 register BOOL found
= NO
;
84 while (*table
&& !found
)
86 if (*table
== '\\') table
+= 2;
89 if (*table
== val
) found
= YES
;
96 /* Remove key and its associated value from the table. */
99 removeKeyFromTable(const char *key
, char *table
)
107 buf
= (char *)malloc(len
+ 3);
109 sprintf(buf
, "\"%s\"", key
);
113 if(strncmp(buf
, tab
, len
) == 0) {
116 while((c
= *(tab
+ len
)) != ';') {
124 if(*(tab
+ len
) == '\n') len
++;
133 if(len
== -1) return NO
;
135 while((*tab
= *(tab
+ len
))) {
148 char *begin
= *list
, *end
;
152 while (*begin
&& newsize
&& isspace(*begin
)) {
157 while (*end
&& newsize
&& !isspace(*end
)) {
163 newstr
= malloc(end
- begin
+ 1);
164 strncpy(newstr
, begin
, end
- begin
);
171 * compress == compress escaped characters to one character
173 int stringLength(char *table
, int compress
)
182 ret
+= 1 + (compress
? 0 : 1);
186 if (*table
== '\"') return ret
;
194 // looks in table for strings of format << "key" = "value"; >>
196 BOOL
getValueForStringTableKey(char *table
, char *key
, char **val
, int *size
)
203 eatThru('\"',&table
);
205 keyLength
= strlen(key
);
207 (stringLength(table
,1) == keyLength
) &&
208 (keyncmp(key
, table
, keyLength
) == 0))
212 /* found the key; now look for either
220 } else if (c
== '=' || c
== ';') {
227 eatThru('\"',&table
);
230 *size
= stringLength(table
,0);
243 * Returns a new malloc'ed string if one is found
244 * in the string table matching 'key'. Also translates
245 * \n escapes in the string.
247 char *newStringForStringTableKey(
252 char *val
, *newstr
, *p
;
255 if (getValueForStringTableKey(table
, key
, &val
, &size
)) {
256 newstr
= malloc(size
+1);
257 for (p
= newstr
; size
; size
--, p
++, val
++) {
258 if ((*p
= *val
) == '\\') {
284 newStringForKey(char *key
)
289 if (getValueForKey(key
, &val
, &size
) && size
) {
290 newstr
= malloc(size
+ 1);
291 strncpy(newstr
, val
, size
);
298 /* parse a command line
299 * in the form: [<argument> ...] [<option>=<value> ...]
300 * both <option> and <value> must be either composed of
301 * non-whitespace characters, or enclosed in quotes.
304 static char *getToken(char *line
, char **begin
, int *len
)
308 while (*line
&& *line
!= '\"')
310 *len
= line
++ - *begin
;
313 while (*line
&& !isspace(*line
) && *line
!= '=')
315 *len
= line
- *begin
;
320 BOOL
getValueForBootKey(char *line
, char *match
, char **matchval
, int *len
)
323 int key_len
, value_len
;
326 /* look for keyword or argument */
327 while (isspace(*line
)) line
++;
329 /* now look for '=' or whitespace */
330 line
= getToken(line
, &key
, &key_len
);
331 /* line now points to '=' or space */
332 if (*line
&& !isspace(*line
)) {
333 line
= getToken(++line
, &value
, &value_len
);
338 if ((strlen(match
) == key_len
)
339 && strncmp(match
, key
, key_len
) == 0) {
355 if (getValueForKey(key
, &val
, &size
) && (size
>= 1) &&
356 val
[0] == 'Y' || val
[0] == 'y')
369 if (getValueForKey(key
, &val
, &size
)) {
370 for (sum
= 0; size
> 0; size
--) {
371 sum
= (sum
* 10) + (*val
++ - '0');
385 if (getValueForBootKey(kernBootStruct
->bootString
, key
, val
, size
))
387 else if (getValueForStringTableKey(kernBootStruct
->config
, key
, val
, size
))
394 #define LOCALIZABLE_PATH \
395 "%s/%s.config/%s.lproj/%s.strings"
397 loadLocalizableStrings(
402 char buf
[256], *config
;
403 register int count
, fd
= -1;
404 char *device_dir
= usrDevices();
406 sprintf(buf
, LOCALIZABLE_PATH
, device_dir
, name
,
407 Language
, tableName
);
408 if ((fd
= open(buf
, 0)) < 0) {
409 sprintf(buf
, LOCALIZABLE_PATH
, device_dir
, name
,
410 "English", tableName
);
411 if ((fd
= open(buf
,0)) < 0) {
415 count
= file_size(fd
);
416 config
= malloc(count
);
417 count
= read(fd
, config
, count
);
434 char *table
, *name
, *version
, *newName
;
435 char *path
= malloc(256);
437 #define LONG_NAME_FORMAT "%s (v%s)"
438 sprintf(path
, "%s/%s.config/%s.table",
439 usrDevices(), bundleName
, tableName
? tableName
: "Default");
440 if (loadConfigFile(path
, &table
, YES
) == 0) {
441 version
= newStringForStringTableKey(table
, "Version");
444 version
= newString("0.0");
446 table
= loadLocalizableStrings(bundleName
,
447 tableName
? tableName
: "Localizable");
449 name
= newStringForStringTableKey(table
, "Long Name");
452 name
= newString(bundleName
);
454 newName
= malloc(strlen(name
)+strlen(version
)+strlen(LONG_NAME_FORMAT
));
455 sprintf(newName
, LONG_NAME_FORMAT
, name
, version
);
456 free(name
); free(version
);
468 char *configPtr
= kernBootStruct
->configEnd
;
469 int len
= strlen(config
);
471 if ((configPtr
- kernBootStruct
->config
) > CONFIG_SIZE
) {
472 error("No room in memory for config files\n");
475 strcpy(configPtr
, config
);
476 configPtr
+= (len
+ 1);
478 kernBootStruct
->configEnd
= configPtr
;
481 #define TABLE_EXPAND_SIZE 192
484 * Returns 0 if file loaded OK,
485 * -1 if file was not loaded
486 * Does not print error messages.
487 * Returns pointer to table in memory in *table.
488 * Allocates an extra number of bytes for table expansion.
491 loadConfigFile( char *configFile
, char **table
, BOOL allocTable
)
493 char *configPtr
= kernBootStruct
->configEnd
;
496 /* Read config file into memory */
497 if ((fd
= open(configFile
, 0)) >= 0)
500 configPtr
= malloc(file_size(fd
)+2+TABLE_EXPAND_SIZE
);
502 if ((configPtr
- kernBootStruct
->config
) > CONFIG_SIZE
) {
503 error("No room in memory for config files\n");
507 verbose("Reading configuration file '%s'.\n",configFile
);
509 if (table
) *table
= configPtr
;
510 count
= read(fd
, configPtr
, IO_CONFIG_DATA_SIZE
);
517 kernBootStruct
->configEnd
= configPtr
;
525 /* Returns 0 if requested config files were loaded,
526 * 1 if default files were loaded,
527 * -1 if no files were loaded.
528 * Prints error message if files cannot be loaded.
533 char *bundleName
, // bundle directory name (e.g. "System")
534 BOOL useDefault
, // use Default.table instead of instance tables
535 char **table
, // returns pointer to config table
536 BOOL allocTable
// malloc the table and return in *table
541 char *device_dir
= usrDevices();
546 // load up to 99 instance tables
551 for (i
=0; i
< max
; i
++) {
552 sprintf(buf
, "%s/%s.config/Instance%d.table",
555 if (useDefault
|| (loadConfigFile(buf
, table
, allocTable
) != 0)) {
557 // couldn't load first instance table;
558 // try the default table
559 sprintf(buf
, "%s/%s.config/%s",
562 IO_DEFAULT_TABLE_FILENAME
);
563 if (loadConfigFile(buf
, table
, allocTable
) == 0) {
567 error("Config file \"%s\" not found\n", buf
);
580 #define USR_SYSTEM_CONFIG \
581 USR_DEVICES "/System.config"
582 #define USR_SYSTEM_DEFAULT_FILE \
583 USR_SYSTEM_CONFIG "/Default.table"
584 #define ARCH_SYSTEM_CONFIG \
585 ARCH_DEVICES "/System.config"
586 #define ARCH_SYSTEM_DEFAULT_FILE \
587 ARCH_SYSTEM_CONFIG "/Default.table"
588 #define SYSTEM_CONFIG "System"
592 static int sysconfig_dev
;
594 /* Returns 0 if requested config files were loaded,
595 * 1 if default files were loaded,
596 * -1 if no files were loaded.
597 * Prints error message if files cannot be loaded.
606 int ret
, len
, doDefault
=0;
607 char *device_dir
= usrDevices();
610 printf("In Load system config which=%d ; size=%d\n", which
, size
);
613 buf
= bp
= malloc(256);
617 printf("In Load system config alt\n");
620 for(cp
= which
, len
= size
; len
&& *cp
&& *cp
!= LP
; cp
++, len
--) ;
622 while (len
-- && *cp
&& *cp
++ != RP
) ;
623 /* cp now points past device */
624 strncpy(buf
,which
,cp
- which
);
631 strcpy(bp
, device_dir
);
632 strcat(bp
, "/System.config/");
633 strncat(bp
, cp
, len
);
634 if (strncmp(cp
+ len
- strlen(IO_TABLE_EXTENSION
),
635 IO_TABLE_EXTENSION
, strlen(IO_TABLE_EXTENSION
)) != 0)
636 strcat(bp
, IO_TABLE_EXTENSION
);
638 strncpy(bp
, cp
, len
);
641 if ((strcmp(bp
, USR_SYSTEM_DEFAULT_FILE
) == 0) ||
642 (strcmp(bp
, ARCH_SYSTEM_DEFAULT_FILE
) == 0))
644 ret
= loadConfigFile(bp
= buf
, 0, 0);
647 printf("In default SYSTEM_CONFIG LOAD\n");
650 ret
= loadConfigDir((bp
= SYSTEM_CONFIG
), 0, 0, 0);
652 printf("come back from SYSTEM_CONFIG loadConfigDir\n");
656 sysconfig_dev
= currentdev();
658 error("System config file '%s' not found\n", bp
);
662 return (ret
< 0 ? ret
: doDefault
);
672 char *path
= malloc(256);
674 char *installVersion
= NULL
, *thisVersion
;
675 char *longName
, *tableName
;
679 int old_dev
= currentdev();
682 switchdev(sysconfig_dev
);
683 if (getValueForKey( "Boot Drivers", &val
, &count
))
686 printf("Loading Boot Drivers\n");
689 while (string
= newStringFromList(&val
, &count
)) {
690 /* Check installation hints... */
691 sprintf(path
, "%s/System.config/" INSTALL_HINTS
692 "/%s.table", usrDevices(), string
);
694 if (getBoolForKey("Ignore Hints") == NO
&&
695 loadConfigFile(path
, &hintTable
, YES
) == 0) {
696 installVersion
= newStringForStringTableKey(
697 hintTable
, "Version");
698 longName
= newStringForStringTableKey(
699 hintTable
, "Long Name");
700 tableName
= newStringForStringTableKey(
701 hintTable
, "Default Table");
704 installVersion
= longName
= tableName
= NULL
;
707 ret
= loadConfigDir(string
, useDefault
, &table
, YES
);
709 thisVersion
= newStringForStringTableKey(
711 if (installVersion
&& thisVersion
&&
712 (strcmp(thisVersion
, installVersion
) != 0)) {
713 /* Versions do not match */
714 driverIsMissing(string
, installVersion
, longName
,
715 tableName
, DRIVER_VERSION_MISMATCH
);
717 struct driver_load_data dl
;
720 if ((openDriverReloc(&dl
)) >= 0) {
721 verbose("Loading binary for %s device driver.\n",string
);
722 if (loadDriver(&dl
) < 0) /// need to stop if error
723 error("Error loading %s device driver.\n",string
);
725 printf("Calling link driver for %s\n", string
);
727 if (linkDriver(&dl
) < 0)
728 error("Error linking %s device Driver.\n",string
);
730 loadConfigDir(string
, useDefault
, NULL
, NO
);
731 driverWasLoaded(string
, table
, NULL
);
734 free(installVersion
); free(longName
);
739 /* driver not found */
740 driverIsMissing(string
, installVersion
, longName
,
741 tableName
, DRIVER_NOT_FOUND
);
745 useDefault
= 1; // use defaults from now on
749 error("Warning: No Boot drivers specified in system config.\n");
752 kernBootStruct
->first_addr0
=
753 (int)kernBootStruct
->configEnd
+ 1024;
758 #endif /* DISABLED */