efi_loader: eliminate inline function ascii2unicode()
authorHeinrich Schuchardt <xypron.glpk@gmx.de>
Sat, 7 Sep 2019 19:05:45 +0000 (21:05 +0200)
committerHeinrich Schuchardt <xypron.glpk@gmx.de>
Mon, 9 Sep 2019 13:21:08 +0000 (15:21 +0200)
ascii2unicode() can only convert characters 0x00-0x7f from UTF-8 to UTF-16.
Use utf8_utf16_strcpy() instead.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
include/efi_loader.h
lib/efi_loader/efi_device_path_to_text.c
lib/efi_loader/efi_file.c

index 00eba8a..dd24a27 100644 (file)
@@ -555,22 +555,6 @@ efi_status_t efi_dp_from_name(const char *dev, const char *devnr,
        (((_dp)->type == DEVICE_PATH_TYPE_##_type) && \
         ((_dp)->sub_type == DEVICE_PATH_SUB_TYPE_##_subtype))
 
-/**
- * ascii2unicode() - convert ASCII string to UTF-16 string
- *
- * A zero terminated ASCII string is converted to a zero terminated UTF-16
- * string. The output buffer must be preassigned.
- *
- * @unicode:   preassigned output buffer for UTF-16 string
- * @ascii:     ASCII string to be converted
- */
-static inline void ascii2unicode(u16 *unicode, const char *ascii)
-{
-       while (*ascii)
-               *(unicode++) = *(ascii++);
-       *unicode = 0;
-}
-
 static inline int guidcmp(const void *g1, const void *g2)
 {
        return memcmp(g1, g2, sizeof(efi_guid_t));
index b20b7c0..0f3796b 100644 (file)
@@ -29,15 +29,15 @@ const efi_guid_t efi_guid_device_path_to_text_protocol =
 static u16 *efi_str_to_u16(char *str)
 {
        efi_uintn_t len;
-       u16 *out;
+       u16 *out, *dst;
        efi_status_t ret;
 
-       len = strlen(str) + 1;
-       ret = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES, len * sizeof(u16),
-                               (void **)&out);
+       len = sizeof(u16) * (utf8_utf16_strlen(str) + 1);
+       ret = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES, len, (void **)&out);
        if (ret != EFI_SUCCESS)
                return NULL;
-       ascii2unicode(out, str);
+       dst = out;
+       utf8_utf16_strcpy(&dst, str);
        return out;
 }
 
index a13754a..9f78b82 100644 (file)
@@ -339,6 +339,7 @@ static efi_status_t dir_read(struct file_handle *fh, u64 *buffer_size,
        struct efi_file_info *info = buffer;
        struct fs_dirent *dent;
        unsigned int required_size;
+       u16 *dst;
 
        if (!fh->dirs) {
                assert(fh->offset == 0);
@@ -381,7 +382,8 @@ static efi_status_t dir_read(struct file_handle *fh, u64 *buffer_size,
        }
 
        /* check buffer size: */
-       required_size = sizeof(*info) + 2 * (strlen(dent->name) + 1);
+       required_size = sizeof(*info) +
+                       2 * (utf8_utf16_strlen(dent->name) + 1);
        if (*buffer_size < required_size) {
                *buffer_size = required_size;
                fh->dent = dent;
@@ -398,7 +400,8 @@ static efi_status_t dir_read(struct file_handle *fh, u64 *buffer_size,
        if (dent->type == FS_DT_DIR)
                info->attribute |= EFI_FILE_DIRECTORY;
 
-       ascii2unicode(info->file_name, dent->name);
+       dst = info->file_name;
+       utf8_utf16_strcpy(&dst, dent->name);
 
        fh->offset++;
 
@@ -577,6 +580,7 @@ static efi_status_t EFIAPI efi_file_getinfo(struct efi_file_handle *file,
 {
        struct file_handle *fh = to_fh(file);
        efi_status_t ret = EFI_SUCCESS;
+       u16 *dst;
 
        EFI_ENTRY("%p, %pUl, %p, %p", file, info_type, buffer_size, buffer);
 
@@ -587,7 +591,8 @@ static efi_status_t EFIAPI efi_file_getinfo(struct efi_file_handle *file,
                loff_t file_size;
 
                /* check buffer size: */
-               required_size = sizeof(*info) + 2 * (strlen(filename) + 1);
+               required_size = sizeof(*info) +
+                               2 * (utf8_utf16_strlen(filename) + 1);
                if (*buffer_size < required_size) {
                        *buffer_size = required_size;
                        ret = EFI_BUFFER_TOO_SMALL;
@@ -613,7 +618,8 @@ static efi_status_t EFIAPI efi_file_getinfo(struct efi_file_handle *file,
                if (fh->isdir)
                        info->attribute |= EFI_FILE_DIRECTORY;
 
-               ascii2unicode(info->file_name, filename);
+               dst = info->file_name;
+               utf8_utf16_strcpy(&dst, filename);
        } else if (!guidcmp(info_type, &efi_file_system_info_guid)) {
                struct efi_file_system_info *info = buffer;
                disk_partition_t part;
@@ -628,8 +634,9 @@ static efi_status_t EFIAPI efi_file_getinfo(struct efi_file_handle *file,
                        ret = EFI_DEVICE_ERROR;
                        goto error;
                }
-               required_size = sizeof(info) + 2 *
-                               (strlen((const char *)part.name) + 1);
+               required_size = sizeof(*info) + 2 *
+                               (utf8_utf16_strlen((const char *)part.name) +
+                                1);
                if (*buffer_size < required_size) {
                        *buffer_size = required_size;
                        ret = EFI_BUFFER_TOO_SMALL;
@@ -647,8 +654,8 @@ static efi_status_t EFIAPI efi_file_getinfo(struct efi_file_handle *file,
                 * TODO: The volume label is not available in U-Boot.
                 * Use the partition name as substitute.
                 */
-               ascii2unicode((u16 *)info->volume_label,
-                             (const char *)part.name);
+               dst = info->volume_label;
+               utf8_utf16_strcpy(&dst, (const char *)part.name);
        } else {
                ret = EFI_UNSUPPORTED;
        }