Refine some internal functions of PCD Driver.
[mirror_edk2.git] / EdkModulePkg / Universal / PCD / Dxe / Pcd.c
1 /** @file
2 PCD DXE driver
3
4 Copyright (c) 2006, Intel Corporation
5 All rights reserved. This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
9
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12
13
14 Module Name: Pcd.c
15
16 **/
17
18 #include "Service.h"
19
20
21 PCD_PROTOCOL mPcdInstance = {
22 DxePcdSetSku,
23
24 DxePcdGet8,
25 DxePcdGet16,
26 DxePcdGet32,
27 DxePcdGet64,
28 DxePcdGetPtr,
29 DxePcdGetBool,
30 DxePcdGetSize,
31
32 DxePcdGet8Ex,
33 DxePcdGet16Ex,
34 DxePcdGet32Ex,
35 DxePcdGet64Ex,
36 DxePcdGetPtrEx,
37 DxePcdGetBoolEx,
38 DxePcdGetSizeEx,
39
40 DxePcdSet8,
41 DxePcdSet16,
42 DxePcdSet32,
43 DxePcdSet64,
44 DxePcdSetPtr,
45 DxePcdSetBool,
46
47 DxePcdSet8Ex,
48 DxePcdSet16Ex,
49 DxePcdSet32Ex,
50 DxePcdSet64Ex,
51 DxePcdSetPtrEx,
52 DxePcdSetBoolEx,
53
54 DxeRegisterCallBackOnSet,
55 DxeUnRegisterCallBackOnSet,
56 DxePcdGetNextToken
57 };
58
59
60 //
61 // Static global to reduce the code size
62 //
63 static EFI_HANDLE NewHandle = NULL;
64
65 EFI_STATUS
66 EFIAPI
67 PcdDxeInit (
68 IN EFI_HANDLE ImageHandle,
69 IN EFI_SYSTEM_TABLE *SystemTable
70 )
71 {
72 EFI_STATUS Status;
73
74 //
75 // Make sure the Pcd Protocol is not already installed in the system
76 //
77
78 ASSERT_PROTOCOL_ALREADY_INSTALLED (NULL, &gPcdProtocolGuid);
79
80 BuildPcdDxeDataBase ();
81
82 //
83 // BugBug Check if PcdDatabase is already installed.
84 //
85
86 Status = gBS->InstallProtocolInterface (
87 &NewHandle,
88 &gPcdProtocolGuid,
89 EFI_NATIVE_INTERFACE,
90 &mPcdInstance
91 );
92
93 ASSERT_EFI_ERROR (Status);
94
95 return EFI_SUCCESS;
96
97 }
98
99
100 VOID
101 EFIAPI
102 DxePcdSetSku (
103 IN SKU_ID SkuId
104 )
105 {
106 mPcdDatabase->PeiDb.Init.SystemSkuId = SkuId;
107
108 return;
109 }
110
111
112
113 UINT8
114 EFIAPI
115 DxePcdGet8 (
116 IN PCD_TOKEN_NUMBER TokenNumber
117 )
118 {
119 return *((UINT8 *) GetWorker (TokenNumber, sizeof (UINT8)));
120 }
121
122
123
124 UINT16
125 EFIAPI
126 DxePcdGet16 (
127 IN PCD_TOKEN_NUMBER TokenNumber
128 )
129 {
130 return ReadUnaligned16 (GetWorker (TokenNumber, sizeof (UINT16)));
131 }
132
133
134
135 UINT32
136 EFIAPI
137 DxePcdGet32 (
138 IN PCD_TOKEN_NUMBER TokenNumber
139 )
140 {
141 return ReadUnaligned32 (GetWorker (TokenNumber, sizeof (UINT32)));
142 }
143
144
145
146 UINT64
147 EFIAPI
148 DxePcdGet64 (
149 IN PCD_TOKEN_NUMBER TokenNumber
150 )
151 {
152 return ReadUnaligned64(GetWorker (TokenNumber, sizeof (UINT64)));
153 }
154
155
156
157 VOID *
158 EFIAPI
159 DxePcdGetPtr (
160 IN PCD_TOKEN_NUMBER TokenNumber
161 )
162 {
163 return GetWorker (TokenNumber, 0);
164 }
165
166
167
168 BOOLEAN
169 EFIAPI
170 DxePcdGetBool (
171 IN PCD_TOKEN_NUMBER TokenNumber
172 )
173 {
174 return *((BOOLEAN *) GetWorker (TokenNumber, sizeof (BOOLEAN)));
175 }
176
177
178
179 UINTN
180 EFIAPI
181 DxePcdGetSize (
182 IN PCD_TOKEN_NUMBER TokenNumber
183 )
184 {
185 UINT16 * SizeTable;
186
187 SizeTable = (TokenNumber < PEI_LOCAL_TOKEN_NUMBER) ? mPcdDatabase->PeiDb.Init.SizeTable :
188 mPcdDatabase->DxeDb.Init.SizeTable;
189
190
191 TokenNumber = (TokenNumber < PEI_LOCAL_TOKEN_NUMBER) ? TokenNumber : (TokenNumber - PEI_LOCAL_TOKEN_NUMBER);
192
193 return SizeTable[TokenNumber];
194 }
195
196
197
198 UINT8
199 EFIAPI
200 DxePcdGet8Ex (
201 IN CONST EFI_GUID *Guid,
202 IN PCD_TOKEN_NUMBER ExTokenNumber
203 )
204 {
205 return *((UINT8 *) ExGetWorker (Guid, ExTokenNumber, sizeof(UINT8)));
206 }
207
208
209
210 UINT16
211 EFIAPI
212 DxePcdGet16Ex (
213 IN CONST EFI_GUID *Guid,
214 IN PCD_TOKEN_NUMBER ExTokenNumber
215 )
216 {
217 return ReadUnaligned16 (ExGetWorker (Guid, ExTokenNumber, sizeof(UINT16)));
218 }
219
220
221
222 UINT32
223 EFIAPI
224 DxePcdGet32Ex (
225 IN CONST EFI_GUID *Guid,
226 IN PCD_TOKEN_NUMBER ExTokenNumber
227 )
228 {
229 return ReadUnaligned32 (ExGetWorker (Guid, ExTokenNumber, sizeof(UINT32)));
230 }
231
232
233
234 UINT64
235 EFIAPI
236 DxePcdGet64Ex (
237 IN CONST EFI_GUID *Guid,
238 IN PCD_TOKEN_NUMBER ExTokenNumber
239 )
240 {
241 return ReadUnaligned64 (ExGetWorker (Guid, ExTokenNumber, sizeof(UINT64)));
242 }
243
244
245
246 VOID *
247 EFIAPI
248 DxePcdGetPtrEx (
249 IN CONST EFI_GUID *Guid,
250 IN PCD_TOKEN_NUMBER ExTokenNumber
251 )
252 {
253 return ExGetWorker (Guid, ExTokenNumber, 0);
254 }
255
256
257
258 BOOLEAN
259 EFIAPI
260 DxePcdGetBoolEx (
261 IN CONST EFI_GUID *Guid,
262 IN PCD_TOKEN_NUMBER ExTokenNumber
263 )
264 {
265 return *((BOOLEAN *) ExGetWorker (Guid, ExTokenNumber, sizeof(BOOLEAN)));
266 }
267
268
269
270 UINTN
271 EFIAPI
272 DxePcdGetSizeEx (
273 IN CONST EFI_GUID *Guid,
274 IN PCD_TOKEN_NUMBER ExTokenNumber
275 )
276 {
277 return DxePcdGetSize(GetExPcdTokenNumber (Guid, ExTokenNumber));
278 }
279
280
281
282 EFI_STATUS
283 EFIAPI
284 DxePcdSet8 (
285 IN PCD_TOKEN_NUMBER TokenNumber,
286 IN UINT8 Value
287 )
288 {
289 return SetWorker (TokenNumber, &Value, sizeof (Value), FALSE);
290 }
291
292
293
294 EFI_STATUS
295 EFIAPI
296 DxePcdSet16 (
297 IN PCD_TOKEN_NUMBER TokenNumber,
298 IN UINT16 Value
299 )
300 {
301 return SetWorker (TokenNumber, &Value, sizeof (Value), FALSE);
302 }
303
304
305
306 EFI_STATUS
307 EFIAPI
308 DxePcdSet32 (
309 IN PCD_TOKEN_NUMBER TokenNumber,
310 IN UINT32 Value
311 )
312 {
313 return SetWorker (TokenNumber, &Value, sizeof (Value), FALSE);
314 }
315
316
317
318 EFI_STATUS
319 EFIAPI
320 DxePcdSet64 (
321 IN PCD_TOKEN_NUMBER TokenNumber,
322 IN UINT64 Value
323 )
324 {
325 return SetWorker (TokenNumber, &Value, sizeof (Value), FALSE);
326 }
327
328
329
330 EFI_STATUS
331 EFIAPI
332 DxePcdSetPtr (
333 IN PCD_TOKEN_NUMBER TokenNumber,
334 IN UINTN SizeOfBuffer,
335 IN VOID *Buffer
336 )
337 {
338 return SetWorker (TokenNumber, Buffer, SizeOfBuffer, TRUE);
339 }
340
341
342
343 EFI_STATUS
344 EFIAPI
345 DxePcdSetBool (
346 IN PCD_TOKEN_NUMBER TokenNumber,
347 IN BOOLEAN Value
348 )
349 {
350 return SetWorker (TokenNumber, &Value, sizeof (Value), FALSE);
351 }
352
353
354
355 EFI_STATUS
356 EFIAPI
357 DxePcdSet8Ex (
358 IN CONST EFI_GUID *Guid,
359 IN PCD_TOKEN_NUMBER ExTokenNumber,
360 IN UINT8 Value
361 )
362 {
363 return ExSetWorker(
364 ExTokenNumber,
365 Guid,
366 &Value,
367 sizeof (Value),
368 FALSE
369 );
370 }
371
372
373
374 EFI_STATUS
375 EFIAPI
376 DxePcdSet16Ex (
377 IN CONST EFI_GUID *Guid,
378 IN PCD_TOKEN_NUMBER ExTokenNumber,
379 IN UINT16 Value
380 )
381 {
382 return ExSetWorker(
383 ExTokenNumber,
384 Guid,
385 &Value,
386 sizeof (Value),
387 FALSE
388 );
389 }
390
391
392
393 EFI_STATUS
394 EFIAPI
395 DxePcdSet32Ex (
396 IN CONST EFI_GUID *Guid,
397 IN PCD_TOKEN_NUMBER ExTokenNumber,
398 IN UINT32 Value
399 )
400 {
401 return ExSetWorker(
402 ExTokenNumber,
403 Guid,
404 &Value,
405 sizeof (Value),
406 FALSE
407 );
408 }
409
410
411
412 EFI_STATUS
413 EFIAPI
414 DxePcdSet64Ex (
415 IN CONST EFI_GUID *Guid,
416 IN PCD_TOKEN_NUMBER ExTokenNumber,
417 IN UINT64 Value
418 )
419 {
420 return ExSetWorker(
421 ExTokenNumber,
422 Guid,
423 &Value,
424 sizeof (Value),
425 FALSE
426 );
427 }
428
429
430
431 EFI_STATUS
432 EFIAPI
433 DxePcdSetPtrEx (
434 IN CONST EFI_GUID *Guid,
435 IN PCD_TOKEN_NUMBER ExTokenNumber,
436 IN UINTN SizeOfBuffer,
437 IN VOID *Buffer
438 )
439 {
440 return ExSetWorker(
441 ExTokenNumber,
442 Guid,
443 Buffer,
444 SizeOfBuffer,
445 TRUE
446 );
447 }
448
449
450
451 EFI_STATUS
452 EFIAPI
453 DxePcdSetBoolEx (
454 IN CONST EFI_GUID *Guid,
455 IN PCD_TOKEN_NUMBER ExTokenNumber,
456 IN BOOLEAN Value
457 )
458 {
459 return ExSetWorker(
460 ExTokenNumber,
461 Guid,
462 &Value,
463 sizeof (Value),
464 TRUE
465 );
466 }
467
468
469
470
471 EFI_STATUS
472 EFIAPI
473 DxeRegisterCallBackOnSet (
474 IN PCD_TOKEN_NUMBER TokenNumber,
475 IN CONST EFI_GUID *Guid, OPTIONAL
476 IN PCD_PROTOCOL_CALLBACK CallBackFunction
477 )
478 {
479 ASSERT (CallBackFunction != NULL);
480
481 return DxeRegisterCallBackWorker (TokenNumber, Guid, CallBackFunction);
482 }
483
484
485
486 EFI_STATUS
487 EFIAPI
488 DxeUnRegisterCallBackOnSet (
489 IN PCD_TOKEN_NUMBER TokenNumber,
490 IN CONST EFI_GUID *Guid, OPTIONAL
491 IN PCD_PROTOCOL_CALLBACK CallBackFunction
492 )
493 {
494 ASSERT (CallBackFunction != NULL);
495
496 return DxeRegisterCallBackWorker (TokenNumber, Guid, CallBackFunction);
497 }
498
499
500
501 EFI_STATUS
502 EFIAPI
503 DxePcdGetNextToken (
504 IN CONST EFI_GUID *Guid, OPTIONAL
505 IN OUT PCD_TOKEN_NUMBER *TokenNumber
506 )
507 {
508 PCD_TOKEN_NUMBER ExTokenNumber;
509
510 //
511 // Scan the local token space
512 //
513 if (Guid == NULL) {
514 *TokenNumber++;
515 if (*TokenNumber == PCD_INVALID_TOKEN_NUMBER) {
516 return EFI_SUCCESS;
517 } else {
518 if (*TokenNumber >= PEI_NEX_TOKEN_NUMBER &&
519 *TokenNumber < PEI_LOCAL_TOKEN_NUMBER) {
520 //
521 // The first Non-Ex type Token Number for DXE PCD
522 // database is PEI_LOCAL_TOKEN_NUMBER
523 //
524 *TokenNumber = PEI_LOCAL_TOKEN_NUMBER;
525 return EFI_SUCCESS;
526 } else if (*TokenNumber >= DXE_NEX_TOKEN_NUMBER + PEI_LOCAL_TOKEN_NUMBER) {
527 *TokenNumber = PCD_INVALID_TOKEN_NUMBER;
528 return EFI_SUCCESS;
529 }
530 }
531 }
532
533 if (PEI_EXMAP_TABLE_EMPTY && PEI_EXMAP_TABLE_EMPTY) {
534 *TokenNumber = (UINTN) PCD_INVALID_TOKEN_NUMBER;
535 return EFI_NOT_FOUND;
536 }
537
538 ExTokenNumber = *TokenNumber;
539 if (!PEI_EXMAP_TABLE_EMPTY) {
540 ExTokenNumber = ExGetNextTokeNumber (
541 Guid,
542 ExTokenNumber,
543 mPcdDatabase->PeiDb.Init.GuidTable,
544 sizeof(mPcdDatabase->PeiDb.Init.GuidTable),
545 mPcdDatabase->PeiDb.Init.ExMapTable,
546 sizeof(mPcdDatabase->PeiDb.Init.ExMapTable)
547 );
548 }
549
550 if (!DXE_EXMAP_TABLE_EMPTY) {
551 ExTokenNumber = ExGetNextTokeNumber (
552 Guid,
553 ExTokenNumber,
554 mPcdDatabase->PeiDb.Init.GuidTable,
555 sizeof(mPcdDatabase->PeiDb.Init.GuidTable),
556 mPcdDatabase->PeiDb.Init.ExMapTable,
557 sizeof(mPcdDatabase->PeiDb.Init.ExMapTable)
558 );
559 }
560
561 *TokenNumber = ExTokenNumber;
562
563 return EFI_SUCCESS;
564 }
565