]> git.proxmox.com Git - mirror_edk2.git/blob - EdkCompatibilityPkg/Compatibility/Uc2OnUcThunk/Uc2OnUcThunk.c
Replace references to RFC 3066 with RFC 4646.
[mirror_edk2.git] / EdkCompatibilityPkg / Compatibility / Uc2OnUcThunk / Uc2OnUcThunk.c
1 /** @file
2 Module produce UC2 on top of UC.
3
4 UEFI 2.1 specification supersedes Inte's EFI Specification 1.10.
5 EFI_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 UC2 on top of UC. This module is used on platform when both of
8 these two conditions are true:
9 1) EFI 1.10 module producing UC present
10 2) And the rest of modules on the platform consume 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 ISO639-2 to RFC4646 and return the converted language.
187 Caller is responsible for freeing the allocated buffer.
188
189 LanguageIso639 contain a single ISO639-2 code such as
190 "eng" or "fra".
191
192 If LanguageIso639 is NULL, then ASSERT.
193 If LanguageRfc4646 is NULL, then ASSERT.
194
195 @param LanguageIso639 ISO639-2 language code.
196
197 @return the allocated buffer or NULL, if the language is not found.
198
199 **/
200 CHAR8*
201 EFIAPI
202 ConvertIso639LanguageToRfc4646Language (
203 IN CONST CHAR8 *LanguageIso639
204 )
205 {
206 UINTN Index;
207 CHAR8 *Rfc4646Language;
208
209 for (Index = 0; Iso639ToRfc4646ConversionTable[Index] != 0; Index += 5) {
210 if (CompareMem (LanguageIso639, &Iso639ToRfc4646ConversionTable[Index], 3) == 0) {
211 Rfc4646Language = AllocateZeroPool (3);
212 if (Rfc4646Language != NULL) {
213 Rfc4646Language = CopyMem (Rfc4646Language, &Iso639ToRfc4646ConversionTable[Index + 3], 2);
214 }
215 return Rfc4646Language;
216 }
217 }
218
219 return NULL;
220 }
221
222 /**
223 Performs a case-insensitive comparison of two Null-terminated Unicode
224 strings.
225
226 @param This Protocol instance pointer.
227 @param Str1 A pointer to a Null-terminated Unicode string.
228 @param Str2 A pointer to a Null-terminated Unicode string.
229
230 @retval 0 Str1 is equivalent to Str2
231 @retval > 0 Str1 is lexically greater than Str2
232 @retval < 0 Str1 is lexically less than Str2
233
234 **/
235 INTN
236 EFIAPI
237 StriColl (
238 IN EFI_UNICODE_COLLATION_PROTOCOL *This,
239 IN CHAR16 *Str1,
240 IN CHAR16 *Str2
241 );
242
243 /**
244 Converts all the Unicode characters in a Null-terminated Unicode string to
245 lower case Unicode characters.
246
247 @param This Protocol instance pointer.
248 @param Str A pointer to a Null-terminated Unicode string.
249
250 **/
251 VOID
252 EFIAPI
253 StrLwr (
254 IN EFI_UNICODE_COLLATION_PROTOCOL *This,
255 IN OUT CHAR16 *Str
256 );
257
258 /**
259 Converts all the Unicode characters in a Null-terminated Unicode string to upper
260 case Unicode characters.
261
262 @param This Protocol instance pointer.
263 @param Str A pointer to a Null-terminated Unicode string.
264
265 **/
266 VOID
267 EFIAPI
268 StrUpr (
269 IN EFI_UNICODE_COLLATION_PROTOCOL *This,
270 IN OUT CHAR16 *Str
271 );
272
273 /**
274 Performs a case-insensitive comparison of a Null-terminated Unicode
275 pattern string and a Null-terminated Unicode string.
276
277 @param This Protocol instance pointer.
278 @param String A pointer to a Null-terminated Unicode string.
279 @param Pattern A pointer to a Null-terminated Unicode pattern string.
280
281 @retval TRUE Pattern was found in String.
282 @retval FALSE Pattern was not found in String.
283
284 **/
285 BOOLEAN
286 EFIAPI
287 MetaiMatch (
288 IN EFI_UNICODE_COLLATION_PROTOCOL *This,
289 IN CHAR16 *String,
290 IN CHAR16 *Pattern
291 );
292
293 /**
294 Converts an 8.3 FAT file name in an OEM character set to a Null-terminated
295 Unicode string.
296
297 @param This Protocol instance pointer.
298 @param FatSize The size of the string Fat in bytes.
299 @param Fat A pointer to a Null-terminated string that contains an 8.3 file
300 name using an OEM character set.
301 @param String A pointer to a Null-terminated Unicode string. The string must
302 be preallocated to hold FatSize Unicode characters.
303
304 **/
305 VOID
306 EFIAPI
307 FatToStr (
308 IN EFI_UNICODE_COLLATION_PROTOCOL *This,
309 IN UINTN FatSize,
310 IN CHAR8 *Fat,
311 OUT CHAR16 *String
312 );
313
314 /**
315 Converts a Null-terminated Unicode string to legal characters in a FAT
316 filename using an OEM character set.
317
318 @param This Protocol instance pointer.
319 @param String A pointer to a Null-terminated Unicode string. The string must
320 be preallocated to hold FatSize Unicode characters.
321 @param FatSize The size of the string Fat in bytes.
322 @param Fat A pointer to a Null-terminated string that contains an 8.3 file
323 name using an OEM character set.
324
325 @retval TRUE Fat is a Long File Name
326 @retval FALSE Fat is an 8.3 file name
327
328 **/
329 BOOLEAN
330 EFIAPI
331 StrToFat (
332 IN EFI_UNICODE_COLLATION_PROTOCOL *This,
333 IN CHAR16 *String,
334 IN UINTN FatSize,
335 OUT CHAR8 *Fat
336 );
337
338 #define UC2_PRIVATE_DATA_SIGNATURE SIGNATURE_32 ('_', 'U', 'C', '2')
339
340 typedef struct {
341 UINT32 Signature;
342 EFI_UNICODE_COLLATION_PROTOCOL UC2;
343 EFI_UNICODE_COLLATION_PROTOCOL *UC;
344 } UC2_PRIVATE_DATA;
345
346 #define UC2_PRIVATE_DATA_FROM_THIS(a) CR (a, UC2_PRIVATE_DATA, UC2, UC2_PRIVATE_DATA_SIGNATURE)
347
348 //
349 // Firmware Volume Protocol template
350 //
351 EFI_EVENT mUc2Registration;
352
353 UC2_PRIVATE_DATA gUC2PrivateDataTemplate = {
354 UC2_PRIVATE_DATA_SIGNATURE,
355 {
356 StriColl,
357 MetaiMatch,
358 StrLwr,
359 StrUpr,
360 FatToStr,
361 StrToFat,
362 NULL
363 },
364 NULL
365 };
366
367 //
368 // Module globals
369 //
370
371 VOID
372 EFIAPI
373 UcNotificationEvent (
374 IN EFI_EVENT Event,
375 IN VOID *Context
376 )
377 {
378 EFI_STATUS Status;
379 UINTN BufferSize;
380 EFI_HANDLE Handle;
381 UC2_PRIVATE_DATA *Private;
382 EFI_UNICODE_COLLATION_PROTOCOL *Uc2;
383
384 while (TRUE) {
385 BufferSize = sizeof (Handle);
386 Status = gBS->LocateHandle (
387 ByRegisterNotify,
388 &gEfiUnicodeCollationProtocolGuid,
389 mUc2Registration,
390 &BufferSize,
391 &Handle
392 );
393 if (EFI_ERROR (Status)) {
394 //
395 // Exit Path of While Loop....
396 //
397 break;
398 }
399
400 //
401 // Skip this handle if the Firmware Volume Protocol is already installed
402 //
403 Status = gBS->HandleProtocol (
404 Handle,
405 &gEfiUnicodeCollation2ProtocolGuid,
406 (VOID **)&Uc2
407 );
408 if (!EFI_ERROR (Status)) {
409 continue;
410 }
411
412 //
413 // Allocate private data structure
414 //
415 Private = AllocateCopyPool (sizeof (UC2_PRIVATE_DATA), &gUC2PrivateDataTemplate);
416 if (Private == NULL) {
417 continue;
418 }
419
420 //
421 // Retrieve the UC Protocol
422 //
423 Status = gBS->HandleProtocol (
424 Handle,
425 &gEfiUnicodeCollationProtocolGuid,
426 (VOID **)&Private->UC
427 );
428 ASSERT_EFI_ERROR (Status);
429
430 //
431 // Fill in rest of private data structure
432 //
433 Private->UC2.SupportedLanguages = ConvertIso639LanguageToRfc4646Language (Private->UC->SupportedLanguages);
434 if (Private->UC2.SupportedLanguages != NULL) {
435
436 //
437 // Install Firmware Volume Protocol onto same handle
438 //
439 Status = gBS->InstallMultipleProtocolInterfaces (
440 &Handle,
441 &gEfiUnicodeCollation2ProtocolGuid,
442 &Private->UC2,
443 NULL
444 );
445 ASSERT_EFI_ERROR (Status);
446 }
447 }
448 }
449
450
451 /**
452 The user Entry Point for DXE driver. The user code starts with this function
453 as the real entry point for the image goes into a library that calls this
454 function.
455
456 @param[in] ImageHandle The firmware allocated handle for the EFI image.
457 @param[in] SystemTable A pointer to the EFI System Table.
458
459 @retval EFI_SUCCESS The entry point is executed successfully.
460 @retval other Some error occurs when executing this entry point.
461
462 **/
463 EFI_STATUS
464 EFIAPI
465 InitializeUC2 (
466 IN EFI_HANDLE ImageHandle,
467 IN EFI_SYSTEM_TABLE *SystemTable
468 )
469 {
470 EfiCreateProtocolNotifyEvent (
471 &gEfiUnicodeCollationProtocolGuid,
472 TPL_CALLBACK,
473 UcNotificationEvent,
474 NULL,
475 &mUc2Registration
476 );
477 return EFI_SUCCESS;
478 }
479
480
481 /**
482 Performs a case-insensitive comparison of two Null-terminated Unicode
483 strings.
484
485 @param This Protocol instance pointer.
486 @param Str1 A pointer to a Null-terminated Unicode string.
487 @param Str2 A pointer to a Null-terminated Unicode string.
488
489 @retval 0 Str1 is equivalent to Str2
490 @retval > 0 Str1 is lexically greater than Str2
491 @retval < 0 Str1 is lexically less than Str2
492
493 **/
494 INTN
495 EFIAPI
496 StriColl (
497 IN EFI_UNICODE_COLLATION_PROTOCOL *This,
498 IN CHAR16 *Str1,
499 IN CHAR16 *Str2
500 )
501 {
502 UC2_PRIVATE_DATA *Private;
503
504 Private = UC2_PRIVATE_DATA_FROM_THIS (This);
505
506 return Private->UC->StriColl (Private->UC, Str1, Str2);
507 }
508
509
510 /**
511 Converts all the Unicode characters in a Null-terminated Unicode string to
512 lower case Unicode characters.
513
514 @param This Protocol instance pointer.
515 @param Str A pointer to a Null-terminated Unicode string.
516
517 **/
518 VOID
519 EFIAPI
520 StrLwr (
521 IN EFI_UNICODE_COLLATION_PROTOCOL *This,
522 IN OUT CHAR16 *Str
523 )
524 {
525 UC2_PRIVATE_DATA *Private;
526
527 Private = UC2_PRIVATE_DATA_FROM_THIS (This);
528
529 Private->UC->StrLwr (Private->UC, Str);
530 }
531
532
533 /**
534 Converts all the Unicode characters in a Null-terminated Unicode string to upper
535 case Unicode characters.
536
537 @param This Protocol instance pointer.
538 @param Str A pointer to a Null-terminated Unicode string.
539
540 **/
541 VOID
542 EFIAPI
543 StrUpr (
544 IN EFI_UNICODE_COLLATION_PROTOCOL *This,
545 IN OUT CHAR16 *Str
546 )
547 {
548 UC2_PRIVATE_DATA *Private;
549
550 Private = UC2_PRIVATE_DATA_FROM_THIS (This);
551
552 Private->UC->StrUpr (Private->UC, Str);
553 }
554
555 /**
556 Performs a case-insensitive comparison of a Null-terminated Unicode
557 pattern string and a Null-terminated Unicode string.
558
559 @param This Protocol instance pointer.
560 @param String A pointer to a Null-terminated Unicode string.
561 @param Pattern A pointer to a Null-terminated Unicode pattern string.
562
563 @retval TRUE Pattern was found in String.
564 @retval FALSE Pattern was not found in String.
565
566 **/
567 BOOLEAN
568 EFIAPI
569 MetaiMatch (
570 IN EFI_UNICODE_COLLATION_PROTOCOL *This,
571 IN CHAR16 *String,
572 IN CHAR16 *Pattern
573 )
574 {
575 UC2_PRIVATE_DATA *Private;
576
577 Private = UC2_PRIVATE_DATA_FROM_THIS (This);
578
579 return Private->UC->MetaiMatch (Private->UC, String, Pattern);
580 }
581
582
583 /**
584 Converts an 8.3 FAT file name in an OEM character set to a Null-terminated
585 Unicode string.
586
587 @param This Protocol instance pointer.
588 @param FatSize The size of the string Fat in bytes.
589 @param Fat A pointer to a Null-terminated string that contains an 8.3 file
590 name using an OEM character set.
591 @param String A pointer to a Null-terminated Unicode string. The string must
592 be preallocated to hold FatSize Unicode characters.
593
594 **/
595 VOID
596 EFIAPI
597 FatToStr (
598 IN EFI_UNICODE_COLLATION_PROTOCOL *This,
599 IN UINTN FatSize,
600 IN CHAR8 *Fat,
601 OUT CHAR16 *String
602 )
603 {
604 UC2_PRIVATE_DATA *Private;
605
606 Private = UC2_PRIVATE_DATA_FROM_THIS (This);
607
608 Private->UC->FatToStr (Private->UC, FatSize, Fat, String);
609 }
610
611
612 /**
613 Converts a Null-terminated Unicode string to legal characters in a FAT
614 filename using an OEM character set.
615
616 @param This Protocol instance pointer.
617 @param String A pointer to a Null-terminated Unicode string. The string must
618 be preallocated to hold FatSize Unicode characters.
619 @param FatSize The size of the string Fat in bytes.
620 @param Fat A pointer to a Null-terminated string that contains an 8.3 file
621 name using an OEM character set.
622
623 @retval TRUE Fat is a Long File Name
624 @retval FALSE Fat is an 8.3 file name
625
626 **/
627 BOOLEAN
628 EFIAPI
629 StrToFat (
630 IN EFI_UNICODE_COLLATION_PROTOCOL *This,
631 IN CHAR16 *String,
632 IN UINTN FatSize,
633 OUT CHAR8 *Fat
634 )
635 {
636 UC2_PRIVATE_DATA *Private;
637
638 Private = UC2_PRIVATE_DATA_FROM_THIS (This);
639
640 return Private->UC->StrToFat (Private->UC, String, FatSize, Fat);
641 }
642