efi_loader: EFI_UNICODE_COLLATION_PROTOCOL

The patch implements the EFI_UNICODE_COLLATION_PROTOCOL.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>
This commit is contained in:
Heinrich Schuchardt 2018-09-04 19:34:58 +02:00 committed by Alexander Graf
parent 1a1012a1c6
commit 0bc4b0da7b
8 changed files with 455 additions and 3 deletions

View File

@ -384,6 +384,8 @@ F: doc/README.uefi
F: doc/README.iscsi F: doc/README.iscsi
F: Documentation/efi.rst F: Documentation/efi.rst
F: include/capitalization.h F: include/capitalization.h
F: include/cp1250.h
F: include/cp437.h
F: include/efi* F: include/efi*
F: include/pe.h F: include/pe.h
F: include/asm-generic/pe.h F: include/asm-generic/pe.h

40
include/cp1250.h Normal file
View File

@ -0,0 +1,40 @@
/* SPDX-License-Identifier: GPL-2.0+ */
/*
* Constant CP1250 contains the Unicode code points for characters 0x80 - 0xff
* of the code page 1250.
*/
#define CP1250 { \
0x20ac, 0x0000, 0x201a, 0x0000, \
0x201e, 0x2026, 0x2020, 0x2021, \
0x0000, 0x2030, 0x0160, 0x2039, \
0x015a, 0x0164, 0x017d, 0x0179, \
0x0000, 0x2018, 0x2019, 0x201c, \
0x201d, 0x2022, 0x2013, 0x2014, \
0x0000, 0x2122, 0x0161, 0x203a, \
0x015b, 0x0165, 0x017e, 0x017a, \
0x00a0, 0x02c7, 0x02d8, 0x0141, \
0x00a4, 0x0104, 0x00a6, 0x00a7, \
0x00a8, 0x00a9, 0x015e, 0x00ab, \
0x00ac, 0x00ad, 0x00ae, 0x017b, \
0x00b0, 0x00b1, 0x02db, 0x0142, \
0x00b4, 0x00b5, 0x00b6, 0x00b7, \
0x00b8, 0x0105, 0x015f, 0x00bb, \
0x013d, 0x02dd, 0x013e, 0x017c, \
0x0154, 0x00c1, 0x00c2, 0x0102, \
0x00c4, 0x0139, 0x0106, 0x00c7, \
0x010c, 0x00c9, 0x0118, 0x00cb, \
0x011a, 0x00cd, 0x00ce, 0x010e, \
0x0110, 0x0143, 0x0147, 0x00d3, \
0x00d4, 0x0150, 0x00d6, 0x00d7, \
0x0158, 0x016e, 0x00da, 0x0170, \
0x00dc, 0x00dd, 0x0162, 0x00df, \
0x0155, 0x00e1, 0x00e2, 0x0103, \
0x00e4, 0x013a, 0x0107, 0x00e7, \
0x010d, 0x00e9, 0x0119, 0x00eb, \
0x011b, 0x00ed, 0x00ee, 0x010f, \
0x0111, 0x0144, 0x0148, 0x00f3, \
0x00f4, 0x0151, 0x00f6, 0x00f7, \
0x0159, 0x016f, 0x00fa, 0x0171, \
0x00fc, 0x00fd, 0x0163, 0x02d9, \
}

40
include/cp437.h Normal file
View File

@ -0,0 +1,40 @@
/* SPDX-License-Identifier: GPL-2.0+ */
/*
* Constant CP437 contains the Unicode code points for characters 0x80 - 0xff
* of the code page 437.
*/
#define CP437 { \
0x00c7, 0x00fc, 0x00e9, 0x00e2, \
0x00e4, 0x00e0, 0x00e5, 0x00e7, \
0x00ea, 0x00eb, 0x00e8, 0x00ef, \
0x00ee, 0x00ec, 0x00c4, 0x00c5, \
0x00c9, 0x00e6, 0x00c6, 0x00f4, \
0x00f6, 0x00f2, 0x00fb, 0x00f9, \
0x00ff, 0x00d6, 0x00dc, 0x00a2, \
0x00a3, 0x00a5, 0x20a7, 0x0192, \
0x00e1, 0x00ed, 0x00f3, 0x00fa, \
0x00f1, 0x00d1, 0x00aa, 0x00ba, \
0x00bf, 0x2310, 0x00ac, 0x00bd, \
0x00bc, 0x00a1, 0x00ab, 0x00bb, \
0x2591, 0x2592, 0x2593, 0x2502, \
0x2524, 0x2561, 0x2562, 0x2556, \
0x2555, 0x2563, 0x2551, 0x2557, \
0x255d, 0x255c, 0x255b, 0x2510, \
0x2514, 0x2534, 0x252c, 0x251c, \
0x2500, 0x253c, 0x255e, 0x255f, \
0x255a, 0x2554, 0x2569, 0x2566, \
0x2560, 0x2550, 0x256c, 0x2567, \
0x2568, 0x2564, 0x2565, 0x2559, \
0x2558, 0x2552, 0x2553, 0x256b, \
0x256a, 0x2518, 0x250c, 0x2588, \
0x2584, 0x258c, 0x2590, 0x2580, \
0x03b1, 0x00df, 0x0393, 0x03c0, \
0x03a3, 0x03c3, 0x00b5, 0x03c4, \
0x03a6, 0x0398, 0x03a9, 0x03b4, \
0x221e, 0x03c6, 0x03b5, 0x2229, \
0x2261, 0x00b1, 0x2265, 0x2264, \
0x2320, 0x2321, 0x00f7, 0x2248, \
0x00b0, 0x2219, 0x00b7, 0x221a, \
0x207f, 0x00b2, 0x25a0, 0x00a0, \
}

View File

@ -31,6 +31,7 @@ enum efi_timer_delay {
EFI_TIMER_RELATIVE = 2 EFI_TIMER_RELATIVE = 2
}; };
#define efi_intn_t ssize_t
#define efi_uintn_t size_t #define efi_uintn_t size_t
typedef uint16_t *efi_string_t; typedef uint16_t *efi_string_t;
@ -958,4 +959,24 @@ struct efi_driver_binding_protocol {
efi_handle_t driver_binding_handle; efi_handle_t driver_binding_handle;
}; };
#define EFI_UNICODE_COLLATION_PROTOCOL2_GUID \
EFI_GUID(0xa4c751fc, 0x23ae, 0x4c3e, \
0x92, 0xe9, 0x49, 0x64, 0xcf, 0x63, 0xf3, 0x49)
struct efi_unicode_collation_protocol {
efi_intn_t (EFIAPI *stri_coll)(
struct efi_unicode_collation_protocol *this, u16 *s1, u16 *s2);
bool (EFIAPI *metai_match)(struct efi_unicode_collation_protocol *this,
const u16 *string, const u16 *patter);
void (EFIAPI *str_lwr)(struct efi_unicode_collation_protocol
*this, u16 *string);
void (EFIAPI *str_upr)(struct efi_unicode_collation_protocol *this,
u16 *string);
void (EFIAPI *fat_to_str)(struct efi_unicode_collation_protocol *this,
efi_uintn_t fat_size, char *fat, u16 *string);
bool (EFIAPI *str_to_fat)(struct efi_unicode_collation_protocol *this,
const u16 *string, efi_uintn_t fat_size,
char *fat);
char *supported_languages;
};
#endif #endif

View File

@ -98,6 +98,9 @@ extern const struct efi_device_path_to_text_protocol efi_device_path_to_text;
/* implementation of the EFI_DEVICE_PATH_UTILITIES_PROTOCOL */ /* implementation of the EFI_DEVICE_PATH_UTILITIES_PROTOCOL */
extern const struct efi_device_path_utilities_protocol extern const struct efi_device_path_utilities_protocol
efi_device_path_utilities; efi_device_path_utilities;
/* Implementation of the EFI_UNICODE_COLLATION_PROTOCOL */
extern const struct efi_unicode_collation_protocol
efi_unicode_collation_protocol;
uint16_t *efi_dp_str(struct efi_device_path *dp); uint16_t *efi_dp_str(struct efi_device_path *dp);
@ -127,6 +130,8 @@ extern const efi_guid_t efi_file_info_guid;
/* GUID for file system information */ /* GUID for file system information */
extern const efi_guid_t efi_file_system_info_guid; extern const efi_guid_t efi_file_system_info_guid;
extern const efi_guid_t efi_guid_device_path_utilities_protocol; extern const efi_guid_t efi_guid_device_path_utilities_protocol;
/* GUID of the Unicode collation protocol */
extern const efi_guid_t efi_guid_unicode_collation_protocol;
extern unsigned int __efi_runtime_start, __efi_runtime_stop; extern unsigned int __efi_runtime_start, __efi_runtime_stop;
extern unsigned int __efi_runtime_rel_start, __efi_runtime_rel_stop; extern unsigned int __efi_runtime_rel_start, __efi_runtime_rel_stop;

View File

@ -17,9 +17,18 @@ always += helloworld.efi
endif endif
obj-$(CONFIG_CMD_BOOTEFI_HELLO) += helloworld_efi.o obj-$(CONFIG_CMD_BOOTEFI_HELLO) += helloworld_efi.o
obj-y += efi_image_loader.o efi_boottime.o efi_runtime.o efi_console.o obj-y += efi_bootmgr.o
obj-y += efi_memory.o efi_device_path_to_text.o efi_device_path.o obj-y += efi_boottime.o
obj-y += efi_device_path_utilities.o efi_file.o efi_variable.o efi_bootmgr.o obj-y += efi_console.o
obj-y += efi_device_path.o
obj-y += efi_device_path_to_text.o
obj-y += efi_device_path_utilities.o
obj-y += efi_file.o
obj-y += efi_image_loader.o
obj-y += efi_memory.o
obj-y += efi_runtime.o
obj-y += efi_unicode_collation.o
obj-y += efi_variable.o
obj-y += efi_watchdog.o obj-y += efi_watchdog.o
obj-$(CONFIG_LCD) += efi_gop.o obj-$(CONFIG_LCD) += efi_gop.o
obj-$(CONFIG_DM_VIDEO) += efi_gop.o obj-$(CONFIG_DM_VIDEO) += efi_gop.o

View File

@ -1526,6 +1526,12 @@ efi_status_t efi_setup_loaded_image(
if (ret != EFI_SUCCESS) if (ret != EFI_SUCCESS)
goto failure; goto failure;
ret = efi_add_protocol(obj->handle,
&efi_guid_unicode_collation_protocol,
(void *)&efi_unicode_collation_protocol);
if (ret != EFI_SUCCESS)
goto failure;
return ret; return ret;
failure: failure:
printf("ERROR: Failure to install protocols for loaded image\n"); printf("ERROR: Failure to install protocols for loaded image\n");

View File

@ -0,0 +1,329 @@
// SPDX-License-Identifier: GPL-2.0+
/*
* EFI Unicode collation protocol
*
* Copyright (c) 2018 Heinrich Schuchardt <xypron.glpk@gmx.de>
*/
#include <common.h>
#include <charset.h>
#include <cp1250.h>
#include <cp437.h>
#include <efi_loader.h>
/* Characters that may not be used in file names */
static const char illegal[] = "<>:\"/\\|?*";
/*
* EDK2 assumes codepage 1250 when creating FAT 8.3 file names.
* Linux defaults to codepage 437 for FAT 8.3 file names.
*/
#if CONFIG_FAT_DEFAULT_CODEPAGE == 1250
/* Unicode code points for code page 1250 characters 0x80 - 0xff */
static const u16 codepage[] = CP1250;
#else
/* Unicode code points for code page 437 characters 0x80 - 0xff */
static const u16 codepage[] = CP437;
#endif
/* GUID of the EFI_UNICODE_COLLATION_PROTOCOL */
const efi_guid_t efi_guid_unicode_collation_protocol =
EFI_UNICODE_COLLATION_PROTOCOL2_GUID;
/**
* efi_stri_coll() - compare utf-16 strings case-insenitively
*
* @this: unicode collation protocol instance
* @s1: first string
* @s2: second string
*
* This function implements the StriColl() service of the
* EFI_UNICODE_COLLATION_PROTOCOL.
*
* See the Unified Extensible Firmware Interface (UEFI) specification for
* details.
*
* TODO:
* The implementation does not follow the Unicode collation algorithm.
* For ASCII characters it results in the same sort order as EDK2.
* We could use table UNICODE_CAPITALIZATION_TABLE for better results.
*
* Return: 0: s1 == s2, > 0: s1 > s2, < 0: s1 < s2
*/
static efi_intn_t EFIAPI efi_stri_coll(
struct efi_unicode_collation_protocol *this, u16 *s1, u16 *s2)
{
s32 c1, c2;
efi_intn_t ret = 0;
EFI_ENTRY("%p, %ls, %ls", this, s1, s2);
for (; *s1 | *s2; ++s1, ++s2) {
c1 = utf_to_upper(*s1);
c2 = utf_to_upper(*s2);
if (c1 < c2) {
ret = -1;
goto out;
} else if (c1 > c2) {
ret = 1;
goto out;
}
}
out:
EFI_EXIT(EFI_SUCCESS);
return ret;
}
/**
* metai_match() - compare utf-16 string with a pattern string case-insenitively
*
* @s: string to compare
* @p: pattern string
*
* The pattern string may use these:
* - * matches >= 0 characters
* - ? matches 1 character
* - [<char1><char2>...<charN>] match any character in the set
* - [<char1>-<char2>] matches any character in the range
*
* This function is called my efi_metai_match().
*
* For '*' pattern searches this function calls itself recursively.
* Performance-wise this is suboptimal, especially for multiple '*' wildcards.
* But it results in simple code.
*
* Return: true if the string is matched.
*/
static bool metai_match(const u16 *s, const u16 *p)
{
u16 first;
for (; *s && *p; ++s, ++p) {
switch (*p) {
case '*':
/* Match 0 or more characters */
++p;
for (;; ++s) {
if (metai_match(s, p))
return true;
if (!*s)
return false;
}
case '?':
/* Match any one character */
break;
case '[':
/* Match any character in the set */
++p;
first = *p;
if (first == ']')
/* Empty set */
return false;
++p;
if (*p == '-') {
/* Range */
++p;
if (*s < first || *s > *p)
return false;
++p;
if (*p != ']')
return false;
} else {
/* Set */
bool hit = false;
if (*s == first)
hit = true;
for (; *p && *p != ']'; ++p) {
if (*p == *s)
hit = true;
}
if (!hit || *p != ']')
return false;
}
break;
default:
/* Match one character */
if (*p != *s)
return false;
}
}
if (!*p && !*s)
return true;
return false;
}
/**
* efi_metai_match() - compare utf-16 string with a pattern string
* case-insenitively
*
* @this: unicode collation protocol instance
* @s: string to compare
* @p: pattern string
*
* The pattern string may use these:
* - * matches >= 0 characters
* - ? matches 1 character
* - [<char1><char2>...<charN>] match any character in the set
* - [<char1>-<char2>] matches any character in the range
*
* This function implements the MetaMatch() service of the
* EFI_UNICODE_COLLATION_PROTOCOL.
*
* Return: true if the string is matched.
*/
static bool EFIAPI efi_metai_match(struct efi_unicode_collation_protocol *this,
const u16 *string, const u16 *pattern)
{
bool ret;
EFI_ENTRY("%p, %ls, %ls", this, string, pattern);
ret = metai_match(string, pattern);
EFI_EXIT(EFI_SUCCESS);
return ret;
}
/**
* efi_str_lwr() - convert to lower case
*
* @this: unicode collation protocol instance
* @string: string to convert
* @p: pattern string
*
* The conversion is done in place. As long as upper and lower letters use the
* same number of words this does not pose a problem.
*
* This function implements the StrLwr() service of the
* EFI_UNICODE_COLLATION_PROTOCOL.
*/
static void EFIAPI efi_str_lwr(struct efi_unicode_collation_protocol *this,
u16 *string)
{
EFI_ENTRY("%p, %ls", this, string);
for (; *string; ++string)
*string = utf_to_lower(*string);
EFI_EXIT(EFI_SUCCESS);
}
/**
* efi_str_upr() - convert to upper case
*
* @this: unicode collation protocol instance
* @string: string to convert
* @p: pattern string
*
* The conversion is done in place. As long as upper and lower letters use the
* same number of words this does not pose a problem.
*
* This function implements the StrUpr() service of the
* EFI_UNICODE_COLLATION_PROTOCOL.
*/
static void EFIAPI efi_str_upr(struct efi_unicode_collation_protocol *this,
u16 *string)
{
EFI_ENTRY("%p, %ls", this, string);
for (; *string; ++string)
*string = utf_to_upper(*string);
EFI_EXIT(EFI_SUCCESS);
}
/**
* efi_fat_to_str() - convert an 8.3 file name from an OEM codepage to Unicode
*
* @this: unicode collation protocol instance
* @fat_size: size of the string to convert
* @fat: string to convert
* @string: converted string
*
* This function implements the FatToStr() service of the
* EFI_UNICODE_COLLATION_PROTOCOL.
*/
static void EFIAPI efi_fat_to_str(struct efi_unicode_collation_protocol *this,
efi_uintn_t fat_size, char *fat, u16 *string)
{
efi_uintn_t i;
u16 c;
EFI_ENTRY("%p, %zu, %s, %p", this, fat_size, fat, string);
for (i = 0; i < fat_size; ++i) {
c = (unsigned char)fat[i];
if (c > 0x80)
c = codepage[i - 0x80];
string[i] = c;
if (!c)
break;
}
string[i] = 0;
EFI_EXIT(EFI_SUCCESS);
}
/**
* efi_fat_to_str() - convert a utf-16 string to legal characters for a FAT
* file name in an OEM code page
*
* @this: unicode collation protocol instance
* @string: Unicode string to convert
* @fat_size: size of the target buffer
* @fat: converted string
*
* This function implements the StrToFat() service of the
* EFI_UNICODE_COLLATION_PROTOCOL.
*
* Return: true if an illegal character was substituted by '_'.
*/
static bool EFIAPI efi_str_to_fat(struct efi_unicode_collation_protocol *this,
const u16 *string, efi_uintn_t fat_size,
char *fat)
{
efi_uintn_t i;
s32 c;
bool ret = false;
EFI_ENTRY("%p, %ls, %zu, %p", this, string, fat_size, fat);
for (i = 0; i < fat_size;) {
c = utf16_get(&string);
switch (c) {
/* Ignore period and space */
case '.':
case ' ':
continue;
case 0:
break;
}
c = utf_to_upper(c);
if (c >= 0x80) {
int j;
/* Look for codepage translation */
for (j = 0; j < 0x80; ++j) {
if (c == codepage[j]) {
c = j + 0x80;
break;
}
}
if (j >= 0x80) {
c = '_';
ret = true;
}
} else if (c && (c < 0x20 || strchr(illegal, c))) {
c = '_';
ret = true;
}
fat[i] = c;
if (!c)
break;
++i;
}
EFI_EXIT(EFI_SUCCESS);
return ret;
}
const struct efi_unicode_collation_protocol efi_unicode_collation_protocol = {
.stri_coll = efi_stri_coll,
.metai_match = efi_metai_match,
.str_lwr = efi_str_lwr,
.str_upr = efi_str_upr,
.fat_to_str = efi_fat_to_str,
.str_to_fat = efi_str_to_fat,
.supported_languages = "en",
};