]> git.proxmox.com Git - mirror_edk2.git/blob - EdkCompatibilityPkg/Compatibility/UcOnUc2Thunk/UcOnUc2Thunk.c
094c36652a6df0b5f687d823e3af157692e2b23c
[mirror_edk2.git] / EdkCompatibilityPkg / Compatibility / UcOnUc2Thunk / UcOnUc2Thunk.c
1 /** @file
2 Module produce UC on top of UC2.
3
4 UEFI 2.1 specification supersedes Inte's EFI Specification 1.10.
5 UNICODE_COLLATION_PROTOCOL defined in Inte's EFI Specification 1.10 is replaced by
6 EFI_UNICODE_COLLATION_PROTOCOL in UEFI 2.1.
7 This module produces UC on top of UC2. This module is used on platform when both of
8 these two conditions are true:
9 1) EFI 1.10 module consuming UC present
10 2) And the rest of modules on the platform produce UC2
11
12 Copyright (c) 2006 - 2008 Intel Corporation. <BR>
13 All rights reserved. This program and the accompanying materials
14 are licensed and made available under the terms and conditions of the BSD License
15 which accompanies this distribution. The full text of the license may be found at
16 http://opensource.org/licenses/bsd-license.php
17
18 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
19 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
20 Module Name:
21
22 **/
23
24 #include <PiDxe.h>
25 #include <Protocol/UnicodeCollation.h>
26 #include <Library/BaseLib.h>
27 #include <Library/DebugLib.h>
28 #include <Library/UefiBootServicesTableLib.h>
29 #include <Library/UefiDriverEntryPoint.h>
30 #include <Library/UefiLib.h>
31 #include <Library/MemoryAllocationLib.h>
32 #include <Library/HiiLib.h>
33 #include <Library/BaseMemoryLib.h>
34
35
36 ///
37 /// The size of a 3 character ISO639 language code.
38 ///
39 #define ISO_639_2_ENTRY_SIZE 3
40
41 //
42 // Lookup table of ISO639-2 3 character language codes to ISO 639-1 2 character language codes
43 // Each entry is 5 CHAR8 values long. The first 3 CHAR8 values are the ISO 639-2 code.
44 // The last 2 CHAR8 values are the ISO 639-1 code.
45 //
46 GLOBAL_REMOVE_IF_UNREFERENCED CONST CHAR8 Iso639ToRfc4646ConversionTable[] =
47 "\
48 aaraa\
49 abkab\
50 afraf\
51 amham\
52 araar\
53 asmas\
54 aymay\
55 azeaz\
56 bakba\
57 belbe\
58 benbn\
59 bihbh\
60 bisbi\
61 bodbo\
62 brebr\
63 bulbg\
64 catca\
65 cescs\
66 corkw\
67 cosco\
68 cymcy\
69 danda\
70 deude\
71 dzodz\
72 ellel\
73 engen\
74 epoeo\
75 estet\
76 euseu\
77 faofo\
78 fasfa\
79 fijfj\
80 finfi\
81 frafr\
82 fryfy\
83 gaiga\
84 gdhgd\
85 glggl\
86 grngn\
87 gujgu\
88 hauha\
89 hebhe\
90 hinhi\
91 hrvhr\
92 hunhu\
93 hyehy\
94 ikuiu\
95 ileie\
96 inaia\
97 indid\
98 ipkik\
99 islis\
100 itait\
101 jawjw\
102 jpnja\
103 kalkl\
104 kankn\
105 kasks\
106 katka\
107 kazkk\
108 khmkm\
109 kinrw\
110 kirky\
111 korko\
112 kurku\
113 laolo\
114 latla\
115 lavlv\
116 linln\
117 litlt\
118 ltzlb\
119 malml\
120 marmr\
121 mkdmk\
122 mlgmg\
123 mltmt\
124 molmo\
125 monmn\
126 mrimi\
127 msams\
128 myamy\
129 nauna\
130 nepne\
131 nldnl\
132 norno\
133 ocioc\
134 ormom\
135 panpa\
136 polpl\
137 porpt\
138 pusps\
139 quequ\
140 rohrm\
141 ronro\
142 runrn\
143 rusru\
144 sagsg\
145 sansa\
146 sinsi\
147 slksk\
148 slvsl\
149 smise\
150 smosm\
151 snasn\
152 sndsd\
153 somso\
154 sotst\
155 spaes\
156 sqisq\
157 srpsr\
158 sswss\
159 sunsu\
160 swasw\
161 swesv\
162 tamta\
163 tattt\
164 telte\
165 tgktg\
166 tgltl\
167 thath\
168 tsnts\
169 tuktk\
170 twitw\
171 uigug\
172 ukruk\
173 urdur\
174 uzbuz\
175 vievi\
176 volvo\
177 wolwo\
178 xhoxh\
179 yidyi\
180 zhaza\
181 zhozh\
182 zulzu\
183 ";
184
185 /**
186 Convert language code from RFC4646 to ISO639-2.
187
188 @param LanguageRfc4646 RFC4646 language code.
189 @param LanguageIso639 ISO639-2 language code.
190
191 @retval EFI_SUCCESS Language code converted.
192 @retval EFI_NOT_FOUND Language code not found.
193
194 **/
195 EFI_STATUS
196 EFIAPI
197 ConvertRfc4646LanguageToIso639Language (
198 IN CHAR8 *LanguageRfc4646,
199 OUT CHAR8 *LanguageIso639
200 )
201 {
202 UINTN Index;
203
204 if ((LanguageRfc4646[2] != '-') && (LanguageRfc4646[2] != 0)) {
205 CopyMem (LanguageIso639, LanguageRfc4646, 3);
206 return EFI_SUCCESS;
207 }
208
209 for (Index = 0; Iso639ToRfc4646ConversionTable[Index] != 0; Index += 5) {
210 if (CompareMem (LanguageRfc4646, &Iso639ToRfc4646ConversionTable[Index + 3], 2) == 0) {
211 CopyMem (LanguageIso639, &Iso639ToRfc4646ConversionTable[Index], 3);
212 return EFI_SUCCESS;
213 }
214 }
215
216 return EFI_NOT_FOUND;
217 }
218
219 /**
220 Performs a case-insensitive comparison of two Null-terminated Unicode
221 strings.
222
223 @param This Protocol instance pointer.
224 @param Str1 A pointer to a Null-terminated Unicode string.
225 @param Str2 A pointer to a Null-terminated Unicode string.
226
227 @retval 0 Str1 is equivalent to Str2
228 @retval > 0 Str1 is lexically greater than Str2
229 @retval < 0 Str1 is lexically less than Str2
230
231 **/
232 INTN
233 EFIAPI
234 StriColl (
235 IN EFI_UNICODE_COLLATION_PROTOCOL *This,
236 IN CHAR16 *Str1,
237 IN CHAR16 *Str2
238 );
239
240 /**
241 Converts all the Unicode characters in a Null-terminated Unicode string to
242 lower case Unicode characters.
243
244 @param This Protocol instance pointer.
245 @param Str A pointer to a Null-terminated Unicode string.
246
247 **/
248 VOID
249 EFIAPI
250 StrLwr (
251 IN EFI_UNICODE_COLLATION_PROTOCOL *This,
252 IN OUT CHAR16 *Str
253 );
254
255 /**
256 Converts all the Unicode characters in a Null-terminated Unicode string to upper
257 case Unicode characters.
258
259 @param This Protocol instance pointer.
260 @param Str A pointer to a Null-terminated Unicode string.
261
262 **/
263 VOID
264 EFIAPI
265 StrUpr (
266 IN EFI_UNICODE_COLLATION_PROTOCOL *This,
267 IN OUT CHAR16 *Str
268 );
269
270 /**
271 Performs a case-insensitive comparison of a Null-terminated Unicode
272 pattern string and a Null-terminated Unicode string.
273
274 @param This Protocol instance pointer.
275 @param String A pointer to a Null-terminated Unicode string.
276 @param Pattern A pointer to a Null-terminated Unicode pattern string.
277
278 @retval TRUE Pattern was found in String.
279 @retval FALSE Pattern was not found in String.
280
281 **/
282 BOOLEAN
283 EFIAPI
284 MetaiMatch (
285 IN EFI_UNICODE_COLLATION_PROTOCOL *This,
286 IN CHAR16 *String,
287 IN CHAR16 *Pattern
288 );
289
290 /**
291 Converts an 8.3 FAT file name in an OEM character set to a Null-terminated
292 Unicode string.
293
294 @param This Protocol instance pointer.
295 @param FatSize The size of the string Fat in bytes.
296 @param Fat A pointer to a Null-terminated string that contains an 8.3 file
297 name using an OEM character set.
298 @param String A pointer to a Null-terminated Unicode string. The string must
299 be preallocated to hold FatSize Unicode characters.
300
301 **/
302 VOID
303 EFIAPI
304 FatToStr (
305 IN EFI_UNICODE_COLLATION_PROTOCOL *This,
306 IN UINTN FatSize,
307 IN CHAR8 *Fat,
308 OUT CHAR16 *String
309 );
310
311 /**
312 Converts a Null-terminated Unicode string to legal characters in a FAT
313 filename using an OEM character set.
314
315 @param This Protocol instance pointer.
316 @param String A pointer to a Null-terminated Unicode string. The string must
317 be preallocated to hold FatSize Unicode characters.
318 @param FatSize The size of the string Fat in bytes.
319 @param Fat A pointer to a Null-terminated string that contains an 8.3 file
320 name using an OEM character set.
321
322 @retval TRUE Fat is a Long File Name
323 @retval FALSE Fat is an 8.3 file name
324
325 **/
326 BOOLEAN
327 EFIAPI
328 StrToFat (
329 IN EFI_UNICODE_COLLATION_PROTOCOL *This,
330 IN CHAR16 *String,
331 IN UINTN FatSize,
332 OUT CHAR8 *Fat
333 );
334
335 #define UC_PRIVATE_DATA_SIGNATURE SIGNATURE_32 ('_', 'U', 'C', '_')
336
337 typedef struct {
338 UINT32 Signature;
339 EFI_UNICODE_COLLATION_PROTOCOL UC;
340 EFI_UNICODE_COLLATION_PROTOCOL *UC2;
341 } UC_PRIVATE_DATA;
342
343 #define UC_PRIVATE_DATA_FROM_THIS(a) CR (a, UC_PRIVATE_DATA, UC, UC_PRIVATE_DATA_SIGNATURE)
344
345 //
346 // Firmware Volume Protocol template
347 //
348 EFI_EVENT mUcRegistration;
349
350 UC_PRIVATE_DATA gUCPrivateDataTemplate = {
351 UC_PRIVATE_DATA_SIGNATURE,
352 {
353 StriColl,
354 MetaiMatch,
355 StrLwr,
356 StrUpr,
357 FatToStr,
358 StrToFat,
359 NULL
360 },
361 NULL
362 };
363
364 //
365 // Module globals
366 //
367
368 VOID
369 EFIAPI
370 Uc2NotificationEvent (
371 IN EFI_EVENT Event,
372 IN VOID *Context
373 )
374 {
375 EFI_STATUS Status;
376 UINTN BufferSize;
377 EFI_HANDLE Handle;
378 UC_PRIVATE_DATA *Private;
379 EFI_UNICODE_COLLATION_PROTOCOL *Uc2;
380
381 while (TRUE) {
382 BufferSize = sizeof (Handle);
383 Status = gBS->LocateHandle (
384 ByRegisterNotify,
385 &gEfiUnicodeCollation2ProtocolGuid,
386 mUcRegistration,
387 &BufferSize,
388 &Handle
389 );
390 if (EFI_ERROR (Status)) {
391 //
392 // Exit Path of While Loop....
393 //
394 break;
395 }
396
397 //
398 // Skip this handle if the Firmware Volume Protocol is already installed
399 //
400 Status = gBS->HandleProtocol (
401 Handle,
402 &gEfiUnicodeCollationProtocolGuid,
403 (VOID **)&Uc2
404 );
405 if (!EFI_ERROR (Status)) {
406 continue;
407 }
408
409 //
410 // Allocate private data structure
411 //
412 Private = AllocateCopyPool (sizeof (UC_PRIVATE_DATA), &gUCPrivateDataTemplate);
413 if (Private == NULL) {
414 continue;
415 }
416
417 //
418 // Retrieve the UC Protocol
419 //
420 Status = gBS->HandleProtocol (
421 Handle,
422 &gEfiUnicodeCollation2ProtocolGuid,
423 (VOID **)&Private->UC2
424 );
425 ASSERT_EFI_ERROR (Status);
426
427 //
428 // Fill in rest of private data structure
429 //
430 Private->UC.SupportedLanguages = AllocateZeroPool (ISO_639_2_ENTRY_SIZE + 1);
431 Status = ConvertRfc4646LanguageToIso639Language (Private->UC2->SupportedLanguages, Private->UC.SupportedLanguages);
432
433 if (!EFI_ERROR (Status)) {
434
435 //
436 // Install Firmware Volume Protocol onto same handle
437 //
438 Status = gBS->InstallMultipleProtocolInterfaces (
439 &Handle,
440 &gEfiUnicodeCollationProtocolGuid,
441 &Private->UC,
442 NULL
443 );
444 ASSERT_EFI_ERROR (Status);
445 }
446 }
447 }
448
449
450 /**
451 The user Entry Point for DXE driver. The user code starts with this function
452 as the real entry point for the image goes into a library that calls this
453 function.
454
455 @param[in] ImageHandle The firmware allocated handle for the EFI image.
456 @param[in] SystemTable A pointer to the EFI System Table.
457
458 @retval EFI_SUCCESS The entry point is executed successfully.
459 @retval other Some error occurs when executing this entry point.
460
461 **/
462 EFI_STATUS
463 EFIAPI
464 InitializeUC (
465 IN EFI_HANDLE ImageHandle,
466 IN EFI_SYSTEM_TABLE *SystemTable
467 )
468 {
469 EfiCreateProtocolNotifyEvent (
470 &gEfiUnicodeCollation2ProtocolGuid,
471 TPL_CALLBACK,
472 Uc2NotificationEvent,
473 NULL,
474 &mUcRegistration
475 );
476 return EFI_SUCCESS;
477 }
478
479
480 /**
481 Performs a case-insensitive comparison of two Null-terminated Unicode
482 strings.
483
484 @param This Protocol instance pointer.
485 @param Str1 A pointer to a Null-terminated Unicode string.
486 @param Str2 A pointer to a Null-terminated Unicode string.
487
488 @retval 0 Str1 is equivalent to Str2
489 @retval > 0 Str1 is lexically greater than Str2
490 @retval < 0 Str1 is lexically less than Str2
491
492 **/
493 INTN
494 EFIAPI
495 StriColl (
496 IN EFI_UNICODE_COLLATION_PROTOCOL *This,
497 IN CHAR16 *Str1,
498 IN CHAR16 *Str2
499 )
500 {
501 UC_PRIVATE_DATA *Private;
502
503 Private = UC_PRIVATE_DATA_FROM_THIS (This);
504
505 return Private->UC2->StriColl (Private->UC2, Str1, Str2);
506 }
507
508
509 /**
510 Converts all the Unicode characters in a Null-terminated Unicode string to
511 lower case Unicode characters.
512
513 @param This Protocol instance pointer.
514 @param Str A pointer to a Null-terminated Unicode string.
515
516 **/
517 VOID
518 EFIAPI
519 StrLwr (
520 IN EFI_UNICODE_COLLATION_PROTOCOL *This,
521 IN OUT CHAR16 *Str
522 )
523 {
524 UC_PRIVATE_DATA *Private;
525
526 Private = UC_PRIVATE_DATA_FROM_THIS (This);
527
528 Private->UC2->StrLwr (Private->UC2, Str);
529 }
530
531
532 /**
533 Converts all the Unicode characters in a Null-terminated Unicode string to upper
534 case Unicode characters.
535
536 @param This Protocol instance pointer.
537 @param Str A pointer to a Null-terminated Unicode string.
538
539 **/
540 VOID
541 EFIAPI
542 StrUpr (
543 IN EFI_UNICODE_COLLATION_PROTOCOL *This,
544 IN OUT CHAR16 *Str
545 )
546 {
547 UC_PRIVATE_DATA *Private;
548
549 Private = UC_PRIVATE_DATA_FROM_THIS (This);
550
551 Private->UC2->StrUpr (Private->UC2, Str);
552 }
553
554 /**
555 Performs a case-insensitive comparison of a Null-terminated Unicode
556 pattern string and a Null-terminated Unicode string.
557
558 @param This Protocol instance pointer.
559 @param String A pointer to a Null-terminated Unicode string.
560 @param Pattern A pointer to a Null-terminated Unicode pattern string.
561
562 @retval TRUE Pattern was found in String.
563 @retval FALSE Pattern was not found in String.
564
565 **/
566 BOOLEAN
567 EFIAPI
568 MetaiMatch (
569 IN EFI_UNICODE_COLLATION_PROTOCOL *This,
570 IN CHAR16 *String,
571 IN CHAR16 *Pattern
572 )
573 {
574 UC_PRIVATE_DATA *Private;
575
576 Private = UC_PRIVATE_DATA_FROM_THIS (This);
577
578 return Private->UC2->MetaiMatch (Private->UC2, String, Pattern);
579 }
580
581
582 /**
583 Converts an 8.3 FAT file name in an OEM character set to a Null-terminated
584 Unicode string.
585
586 @param This Protocol instance pointer.
587 @param FatSize The size of the string Fat in bytes.
588 @param Fat A pointer to a Null-terminated string that contains an 8.3 file
589 name using an OEM character set.
590 @param String A pointer to a Null-terminated Unicode string. The string must
591 be preallocated to hold FatSize Unicode characters.
592
593 **/
594 VOID
595 EFIAPI
596 FatToStr (
597 IN EFI_UNICODE_COLLATION_PROTOCOL *This,
598 IN UINTN FatSize,
599 IN CHAR8 *Fat,
600 OUT CHAR16 *String
601 )
602 {
603 UC_PRIVATE_DATA *Private;
604
605 Private = UC_PRIVATE_DATA_FROM_THIS (This);
606
607 Private->UC2->FatToStr (Private->UC2, FatSize, Fat, String);
608 }
609
610
611 /**
612 Converts a Null-terminated Unicode string to legal characters in a FAT
613 filename using an OEM character set.
614
615 @param This Protocol instance pointer.
616 @param String A pointer to a Null-terminated Unicode string. The string must
617 be preallocated to hold FatSize Unicode characters.
618 @param FatSize The size of the string Fat in bytes.
619 @param Fat A pointer to a Null-terminated string that contains an 8.3 file
620 name using an OEM character set.
621
622 @retval TRUE Fat is a Long File Name
623 @retval FALSE Fat is an 8.3 file name
624
625 **/
626 BOOLEAN
627 EFIAPI
628 StrToFat (
629 IN EFI_UNICODE_COLLATION_PROTOCOL *This,
630 IN CHAR16 *String,
631 IN UINTN FatSize,
632 OUT CHAR8 *Fat
633 )
634 {
635 UC_PRIVATE_DATA *Private;
636
637 Private = UC_PRIVATE_DATA_FROM_THIS (This);
638
639 return Private->UC2->StrToFat (Private->UC2, String, FatSize, Fat);
640 }
641