aeinitfile.c revision 1.1.1.9 1 /******************************************************************************
2 *
3 * Module Name: aeinitfile - Support for optional initialization file
4 *
5 *****************************************************************************/
6
7 /*
8 * Copyright (C) 2000 - 2018, Intel Corp.
9 * All rights reserved.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions, and the following disclaimer,
16 * without modification.
17 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18 * substantially similar to the "NO WARRANTY" disclaimer below
19 * ("Disclaimer") and any redistribution must be conditioned upon
20 * including a substantially similar Disclaimer requirement for further
21 * binary redistribution.
22 * 3. Neither the names of the above-listed copyright holders nor the names
23 * of any contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * Alternatively, this software may be distributed under the terms of the
27 * GNU General Public License ("GPL") version 2 as published by the Free
28 * Software Foundation.
29 *
30 * NO WARRANTY
31 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
34 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41 * POSSIBILITY OF SUCH DAMAGES.
42 */
43
44 #include "aecommon.h"
45 #include "acdispat.h"
46
47 #define _COMPONENT ACPI_TOOLS
48 ACPI_MODULE_NAME ("aeinitfile")
49
50
51 /* Local prototypes */
52
53 static void
54 AeEnterInitFileEntry (
55 INIT_FILE_ENTRY InitEntry,
56 ACPI_WALK_STATE *WalkState);
57
58
59 #define AE_FILE_BUFFER_SIZE 512
60
61 static char LineBuffer[AE_FILE_BUFFER_SIZE];
62 static char NameBuffer[AE_FILE_BUFFER_SIZE];
63 static char ValueBuffer[AE_FILE_BUFFER_SIZE];
64 static FILE *InitFile;
65
66
67 /******************************************************************************
68 *
69 * FUNCTION: AeOpenInitializationFile
70 *
71 * PARAMETERS: Filename - Path to the init file
72 *
73 * RETURN: Status
74 *
75 * DESCRIPTION: Open the initialization file for the -fi option
76 *
77 *****************************************************************************/
78
79 int
80 AeOpenInitializationFile (
81 char *Filename)
82 {
83
84 InitFile = fopen (Filename, "r");
85 if (!InitFile)
86 {
87 fprintf (stderr,
88 "Could not open initialization file: %s\n", Filename);
89 return (-1);
90 }
91
92 AcpiOsPrintf ("Opened initialization file [%s]\n", Filename);
93 return (0);
94 }
95
96
97 /******************************************************************************
98 *
99 * FUNCTION: AeProcessInitFile
100 *
101 * PARAMETERS: None
102 *
103 * RETURN: None
104 *
105 * DESCRIPTION: Read the initialization file and perform all namespace
106 * initializations. AcpiGbl_InitEntries will be used for region
107 * field initialization.
108 *
109 * NOTE: The format of the file is multiple lines, each of format:
110 * <ACPI-pathname> <Integer Value>
111 *
112 *****************************************************************************/
113
114 void
115 AeProcessInitFile(
116 void)
117 {
118 ACPI_WALK_STATE *WalkState;
119 int i;
120 UINT64 idx;
121 ACPI_STATUS Status;
122
123
124 if (!InitFile)
125 {
126 return;
127 }
128
129 /* Create needed objects to be reused for each init entry */
130
131 WalkState = AcpiDsCreateWalkState (0, NULL, NULL, NULL);
132 NameBuffer[0] = '\\';
133
134 while (fgets (LineBuffer, AE_FILE_BUFFER_SIZE, InitFile) != NULL)
135 {
136 ++AcpiGbl_InitFileLineCount;
137 }
138 rewind (InitFile);
139
140 AcpiGbl_InitEntries =
141 AcpiOsAllocate (sizeof (INIT_FILE_ENTRY) * AcpiGbl_InitFileLineCount);
142 for (idx = 0; fgets (LineBuffer, AE_FILE_BUFFER_SIZE, InitFile); ++idx)
143 {
144 if (sscanf (LineBuffer, "%s %s\n",
145 &NameBuffer[1], ValueBuffer) != 2)
146 {
147 goto CleanupAndExit;
148 }
149
150 /* Add a root prefix if not present in the string */
151
152 i = 0;
153 if (NameBuffer[1] == '\\')
154 {
155 i = 1;
156 }
157
158 AcpiGbl_InitEntries[idx].Name =
159 AcpiOsAllocateZeroed (strnlen (NameBuffer + i, AE_FILE_BUFFER_SIZE) + 1);
160
161 strcpy (AcpiGbl_InitEntries[idx].Name, NameBuffer + i);
162
163 Status = AcpiUtStrtoul64 (ValueBuffer, &AcpiGbl_InitEntries[idx].Value);
164 if (ACPI_FAILURE (Status))
165 {
166 AcpiOsPrintf ("%s %s\n", ValueBuffer,
167 AcpiFormatException (Status));
168 goto CleanupAndExit;
169 }
170
171 AeEnterInitFileEntry (AcpiGbl_InitEntries[idx], WalkState);
172 }
173
174 /* Cleanup */
175
176 CleanupAndExit:
177 fclose (InitFile);
178 AcpiDsDeleteWalkState (WalkState);
179 }
180
181
182 /******************************************************************************
183 *
184 * FUNCTION: AeInitFileEntry
185 *
186 * PARAMETERS: InitEntry - Entry of the init file
187 * WalkState - Used for the Store operation
188 *
189 * RETURN: None
190 *
191 * DESCRIPTION: Perform initialization of a single namespace object
192 *
193 * Note: namespace of objects are limited to integers and region
194 * fields units of 8 bytes at this time.
195 *
196 *****************************************************************************/
197
198 static void
199 AeEnterInitFileEntry (
200 INIT_FILE_ENTRY InitEntry,
201 ACPI_WALK_STATE *WalkState)
202 {
203 char *Pathname = InitEntry.Name;
204 UINT64 Value = InitEntry.Value;
205 ACPI_OPERAND_OBJECT *ObjDesc;
206 ACPI_NAMESPACE_NODE *NewNode;
207 ACPI_STATUS Status;
208
209
210 AcpiOsPrintf ("Initializing namespace element: %s\n", Pathname);
211 Status = AcpiNsLookup (NULL, Pathname, ACPI_TYPE_INTEGER,
212 ACPI_IMODE_LOAD_PASS2, ACPI_NS_ERROR_IF_FOUND | ACPI_NS_NO_UPSEARCH |
213 ACPI_NS_EARLY_INIT, NULL, &NewNode);
214 if (ACPI_FAILURE (Status))
215 {
216 ACPI_EXCEPTION ((AE_INFO, Status,
217 "While creating name from namespace initialization file: %s",
218 Pathname));
219 return;
220 }
221
222 ObjDesc = AcpiUtCreateIntegerObject (Value);
223
224 AcpiOsPrintf ("New value: 0x%8.8X%8.8X\n",
225 ACPI_FORMAT_UINT64 (Value));
226
227 /* Store pointer to value descriptor in the Node */
228
229 Status = AcpiNsAttachObject (NewNode, ObjDesc,
230 ACPI_TYPE_INTEGER);
231
232 /* Remove local reference to the object */
233
234 AcpiUtRemoveReference (ObjDesc);
235 }
236
237
238 /******************************************************************************
239 *
240 * FUNCTION: AeLookupInitFileEntry
241 *
242 * PARAMETERS: Pathname - AML namepath in external format
243 * ValueString - value of the namepath if it exitst
244 *
245 * RETURN: None
246 *
247 * DESCRIPTION: Search the init file for a particular name and its value.
248 *
249 *****************************************************************************/
250
251 ACPI_STATUS
252 AeLookupInitFileEntry (
253 char *Pathname,
254 UINT64 *Value)
255 {
256 UINT32 i;
257
258 if (!AcpiGbl_InitEntries)
259 {
260 return AE_NOT_FOUND;
261 }
262
263 for (i = 0; i < AcpiGbl_InitFileLineCount; ++i)
264 {
265 if (!strcmp(AcpiGbl_InitEntries[i].Name, Pathname))
266 {
267 *Value = AcpiGbl_InitEntries[i].Value;
268 return AE_OK;
269 }
270 }
271 return AE_NOT_FOUND;
272 }
273