File: Init.c

package info (click to toggle)
edk2 0~20131112.2590861a-3
  • links: PTS, VCS
  • area: non-free
  • in suites: jessie, jessie-kfreebsd
  • size: 125,836 kB
  • ctags: 175,818
  • sloc: ansic: 1,274,042; python: 75,968; asm: 68,082; perl: 22,386; cpp: 22,278; makefile: 14,914; sh: 4,113; pascal: 1,126; xml: 318; lisp: 24
file content (437 lines) | stat: -rw-r--r-- 11,663 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
/*++

Copyright (c) 2005 - 2013, Intel Corporation                                                         
All rights reserved. This program and the accompanying materials                          
are licensed and made available under the terms and conditions of the BSD License         
which accompanies this distribution. The full text of the license may be found at         
http://opensource.org/licenses/bsd-license.php                                            
                                                                                          
THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,                     
WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.             

Module Name:
  
  Init.c


Abstract:

Revision History

--*/

#include "EfiShellLib.h"

extern EFI_UNICODE_COLLATION_PROTOCOL LibStubUnicodeInterface;

extern EFI_GUID                       ShellEnvProtocol;

EFI_STATUS
InitializeUnicodeCollationSupportWorker (
  IN EFI_HANDLE         AgentHandle,
  IN EFI_GUID           *ProtocolGuid
  )
/*++

Routine Description:

  Worker function to initialize Unicode Collation support.
  It tries to locate Unicode Collation (2) protocol and matches it with current
  platform language code and the default language code.
    
Arguments:

  AgentHandle  - The handle used to open Unicode Collation (2) protocol.
  ProtocolGuid - The pointer to Unicode Collation (2) protocol GUID.
    
Returns:

  EFI_SUCCESS  - The Unicode Collation (2) protocol has been successfully located.
  Others       - The Unicode Collation (2) protocol has not been located.

--*/
{
  EFI_STATUS                      ReturnStatus;
  EFI_STATUS                      Status;
  UINTN                           NumHandles;
  UINTN                           Index;
  EFI_HANDLE                      *Handles;
  EFI_UNICODE_COLLATION_PROTOCOL  *Uci;
  BOOLEAN                         Iso639Language;
  CHAR8                           *BestLanguage;

  Status = BS->LocateHandleBuffer (
                 ByProtocol,
                 ProtocolGuid,
                 NULL,
                 &NumHandles,
                 &Handles
                 );
  if (EFI_ERROR (Status)) {
    return Status;
  }

  Iso639Language = (BOOLEAN) (ProtocolGuid == &gEfiUnicodeCollationProtocolGuid);

  ReturnStatus = EFI_UNSUPPORTED;
  for (Index = 0; Index < NumHandles; Index++) {
    //
    // Open Unicode Collation Protocol
    //
    Status = BS->OpenProtocol (
                   Handles[Index],
                   ProtocolGuid,
                   (VOID **) &Uci,
                   AgentHandle,
                   NULL,
                   EFI_OPEN_PROTOCOL_GET_PROTOCOL
                   );
    if (EFI_ERROR (Status)) {
      continue;
    }

    //
    // Find the best matching matching language from the supported languages
    // of Unicode Collation (2) protocol. 
    //
    BestLanguage = LibSelectBestLanguage (
                     Uci->SupportedLanguages,
                     Iso639Language,
                     NULL
                     );
    if (BestLanguage != NULL) {
      FreePool (BestLanguage);
      UnicodeInterface = Uci;
      ReturnStatus = EFI_SUCCESS;
      break;
    }
  }

  FreePool (Handles);

  return ReturnStatus;
}


EFI_STATUS
InitializeUnicodeCollationSupport (
  IN EFI_HANDLE    AgentHandle
  )
/*++

Routine Description:

  This function initializes Unicode Collation Support for shell.
  It tries to locate Unicode Collation (2) protocol and matches it with current
  platform language code and the default language code.
    
Arguments:

  AgentHandle  - The handle used to open Unicode Collation (2) protocol.
    
Returns:

  EFI_SUCCESS  - The Unicode Collation (2) protocol has been successfully located.
  Others       - The Unicode Collation (2) protocol has not been located.

--*/
{

  EFI_STATUS       Status;

  //
  // First try to use RFC 4646 Unicode Collation 2 Protocol.
  //
  Status = InitializeUnicodeCollationSupportWorker (AgentHandle, &gEfiUnicodeCollation2ProtocolGuid);
  //
  // If the attempt to use Unicode Collation 2 Protocol fails, then we fall back
  // on the ISO 639-2 Unicode Collation Protocol.
  //
  if (EFI_ERROR (Status)) {
    Status = InitializeUnicodeCollationSupportWorker (AgentHandle, &gEfiUnicodeCollationProtocolGuid);
  }

  return Status;
}

VOID
InitializeShellLib (
  IN EFI_HANDLE           ImageHandle,
  IN EFI_SYSTEM_TABLE     *SystemTable
  )
/*++

Routine Description:

  Initializes EFI library for use
    
Arguments:

  ImageHandle - Image handle
  SystemTable - Firmware's EFI system table
    
Returns:

  None

--*/
{
  EFI_LOADED_IMAGE_PROTOCOL *LoadedImage;
  EFI_STATUS                Status;

  if (!ShellLibInitialized) {
    ShellLibInitialized = TRUE;

    //
    // Set up global pointer to the system table, boot services table,
    // and runtime services table
    //
    ST  = SystemTable;
    BS  = SystemTable->BootServices;
    RT  = SystemTable->RuntimeServices;

    //
    // Initialize pool allocation type
    //
    if (ImageHandle) {
      Status = BS->HandleProtocol (
                    ImageHandle,
                    &gEfiLoadedImageProtocolGuid,
                    (VOID *) &LoadedImage
                    );

      if (!EFI_ERROR (Status)) {
        PoolAllocationType = LoadedImage->ImageDataType;
      }

    }
    //
    // Initialize Guid table
    //
    InitializeLibPlatform (ImageHandle, SystemTable);
  }

  if ((ImageHandle != NULL) && (UnicodeInterface == &LibStubUnicodeInterface)) {
    InitializeUnicodeCollationSupport (ImageHandle);
  }

  DEBUG_CODE (
    EFIDebugVariable ();
    EfiDebugAssertInit ();
  )
}


#if (EFI_SPECIFICATION_VERSION < 0x0002000A)
EFI_HII_PROTOCOL *HiiProt        = NULL;
#endif
EFI_HII_HANDLE   HiiLibHandle    = (EFI_HII_HANDLE) 0;
BOOLEAN          HiiInitialized  = FALSE;
UINTN            NumStrings      = 0;
EFI_GUID         SESGuid         = EFI_SE_EXT_SIGNATURE_GUID;

EFI_STATUS
LibInitializeStrings (
  OUT EFI_HII_HANDLE    *HiiHandle,
  IN UINT8              *StringPack,
  IN EFI_GUID           *StringPackGuid
  )
/*++

Routine Description:
  Register our included string package to HII and return the HII handle to the data.
  If previously registered, simply return the handle.

Arguments:
  HiiLibHandle    - A pointer to the handle which is used to reference our string data.
  StringPack      - String package
  StringPackGuid  - String package guid

Returns:
  EFI_SUCCESS     - Command completed successfully

--*/
{
  EFI_STATUS                  Status;
#if (EFI_SPECIFICATION_VERSION >= 0x0002000A)
  EFI_HII_PACKAGE_LIST_HEADER *PackageList;
  EFI_GUID                    PackageListGuid;
  UINT64                      MonotonicCount;  
#else
  EFI_HII_PACKAGES            *HiiPackages;
  VOID                        **Package;
#endif

  ASSERT (HiiHandle);
  
  //
  // If we've already initialized once, then don't do it again. This is
  // done to support building shell commands standalone.
  //
  if (HiiInitialized == TRUE) {
    NumStrings++;
    *HiiHandle = HiiLibHandle;
    return EFI_SUCCESS;
  }

  HiiInitialized = TRUE;
  
#if (EFI_SPECIFICATION_VERSION >= 0x0002000A)
  LocateHiiProtocols ();
  //
  // Update the incoming StringPackGuid to make it unique to be a GUID of a 
  // package list. 
  //  
  CopyMem (&PackageListGuid, StringPackGuid, sizeof (EFI_GUID));
  BS->GetNextMonotonicCount (&MonotonicCount);
  MonotonicCount += *((UINT64 *) (PackageListGuid.Data4));
  CopyMem (PackageListGuid.Data4, &MonotonicCount, sizeof (UINT64));

  PackageList = PreparePackageList (1, &PackageListGuid, StringPack);
  ASSERT (PackageList != NULL);
  Status = gLibHiiDatabase->NewPackageList (
                              gLibHiiDatabase,
                              PackageList,
                              NULL,
                              HiiHandle
                              );
  FreePool (PackageList);
  
#else
  //
  // Find the HII protocol
  //
  Status = LibLocateProtocol (&gEfiHiiProtocolGuid, (VOID **) &HiiProt);
  if (EFI_ERROR (Status)) {
    return Status;
  }

  HiiPackages = AllocateZeroPool (sizeof (EFI_HII_PACKAGES) + sizeof (VOID *));
  if (HiiPackages == NULL) {
    return EFI_OUT_OF_RESOURCES;
  }

  HiiPackages->GuidId           = StringPackGuid;
  HiiPackages->NumberOfPackages = 1;
  Package                       = (VOID **) (((UINT8 *) HiiPackages) + sizeof (EFI_HII_PACKAGES));
  *Package                      = (EFI_HII_STRING_PACK *) StringPack;
  Status                        = HiiProt->NewPack (HiiProt, HiiPackages, HiiHandle);  
  FreePool (HiiPackages);
  
#endif  
  NumStrings++;
  HiiLibHandle = *HiiHandle;
  return Status;
}

EFI_STATUS
LibUnInitializeStrings (
  VOID
  )
{
  EFI_STATUS  Status;

  Status = EFI_SUCCESS;
  NumStrings--;
#if (EFI_SPECIFICATION_VERSION >= 0x0002000A)
  LocateHiiProtocols ();
  if (gLibHiiDatabase != NULL && NumStrings == 0) {
    Status = gLibHiiDatabase->RemovePackageList (gLibHiiDatabase, HiiLibHandle);
  }
#else
  if (HiiProt != NULL && NumStrings == 0) {
    Status = HiiProt->RemovePack (HiiProt, HiiLibHandle);
  }  
#endif
  return Status;
}

EFI_STATUS
LibInitializeShellApplication (
  IN EFI_HANDLE                   ImageHandle,
  IN EFI_SYSTEM_TABLE             *SystemTable
  )
{
  EFI_STATUS  Status;
  EFI_HANDLE  *HandleBuffer;
  UINTN       HandleNum;
  UINTN       HandleIndex;
  //
  // Shell app lib is a super set of the default lib.
  // Initialize the default lib first
  //
  InitializeShellLib (ImageHandle, SystemTable);
  //
  // Connect to the shell interface
  //
  Status = BS->HandleProtocol (ImageHandle, &ShellInterfaceProtocol, (VOID *) &SI);
  if (EFI_ERROR (Status)) {
    DEBUG ((EFI_D_ERROR, "InitShellApp: Application not started from Shell\n"));
    BS->Exit (ImageHandle, Status, 0, NULL);
  }
  //
  // Connect to the shell environment
  //
  Status = BS->HandleProtocol (
                ImageHandle,
                &ShellEnvProtocol,
                (VOID *) &SE2
                );
  if (EFI_ERROR (Status) || !(CompareGuid (&SE2->SESGuid, &SESGuid) == 0 &&
    (SE2->MajorVersion > EFI_SHELL_MAJOR_VER ||
      (SE2->MajorVersion == EFI_SHELL_MAJOR_VER && SE2->MinorVersion >= EFI_SHELL_MINOR_VER))
    )
  ) {
    Status = LibLocateHandle (
              ByProtocol,
              &ShellEnvProtocol,
              NULL,
              &HandleNum,
              &HandleBuffer
              );
    if (EFI_ERROR (Status)) {
      DEBUG ((EFI_D_ERROR, "InitShellApp: Shell environment interfaces not found\n"));
      BS->Exit (ImageHandle, Status, 0, NULL);
    }

    Status = EFI_NOT_FOUND;
    for (HandleIndex = 0; HandleIndex < HandleNum; HandleIndex++) {
      BS->HandleProtocol (
           HandleBuffer[HandleIndex],
           &ShellEnvProtocol,
           (VOID *) &SE2
           );
      if (CompareGuid (&SE2->SESGuid, &SESGuid) == 0 &&
        (SE2->MajorVersion > EFI_SHELL_MAJOR_VER ||
          (SE2->MajorVersion == EFI_SHELL_MAJOR_VER && SE2->MinorVersion >= EFI_SHELL_MINOR_VER)
        )
      ) {
        Status = EFI_SUCCESS;
        break;
      }
    }

    FreePool (HandleBuffer);

    if (EFI_ERROR (Status)) {
      DEBUG ((EFI_D_ERROR, "InitShellApp: Shell environment interfaces not found\n"));
      BS->Exit (ImageHandle, Status, Status, NULL);
    }
  }

  SE = (EFI_SHELL_ENVIRONMENT *) SE2;
  //
  // Disable the page break output mode in default
  //
  DisablePageBreak ();
  
  //
  // Disable the tab key pause output mode in default
  DisableOutputTabPause ();
  
  //
  // Done with init
  //
  return Status;
}