Merge tag 'u-boot-atmel-fixes-2021.01-b' of https://gitlab.denx.de/u-boot/custodians...
[platform/kernel/u-boot.git] / lib / efi_loader / efi_file.c
index beb4fba..72b7ec1 100644 (file)
@@ -1,20 +1,25 @@
 // SPDX-License-Identifier: GPL-2.0+
 /*
- *  EFI utils
+ * EFI_FILE_PROTOCOL
  *
- *  Copyright (c) 2017 Rob Clark
+ * Copyright (c) 2017 Rob Clark
  */
 
 #include <common.h>
 #include <charset.h>
 #include <efi_loader.h>
+#include <log.h>
 #include <malloc.h>
 #include <mapmem.h>
 #include <fs.h>
+#include <part.h>
 
 /* GUID for file system information */
 const efi_guid_t efi_file_system_info_guid = EFI_FILE_SYSTEM_INFO_GUID;
 
+/* GUID to obtain the volume label */
+const efi_guid_t efi_system_volume_label_id = EFI_FILE_SYSTEM_VOLUME_LABEL_ID;
+
 struct file_system {
        struct efi_simple_file_system_protocol base;
        struct efi_device_path *dp;
@@ -28,6 +33,7 @@ struct file_handle {
        struct file_system *fs;
        loff_t offset;       /* current file position/cursor */
        int isdir;
+       u64 open_mode;
 
        /* for reading a directory: */
        struct fs_dir_stream *dirs;
@@ -135,6 +141,25 @@ static int sanitize_path(char *path)
 }
 
 /**
+ * efi_create_file() - create file or directory
+ *
+ * @fh:                        file handle
+ * @attributes:                attributes for newly created file
+ * Returns:            0 for success
+ */
+static int efi_create_file(struct file_handle *fh, u64 attributes)
+{
+       loff_t actwrite;
+       void *buffer = &actwrite;
+
+       if (attributes & EFI_FILE_DIRECTORY)
+               return fs_mkdir(fh->path);
+       else
+               return fs_write(fh->path, map_to_sysmem(buffer), 0, 0,
+                               &actwrite);
+}
+
+/**
  * file_open() - open a file handle
  *
  * @fs:                        file system
@@ -142,13 +167,13 @@ static int sanitize_path(char *path)
  * @file_name:         path of the file to be opened. '\', '.', or '..' may
  *                     be used as modifiers. A leading backslash indicates an
  *                     absolute path.
- * @mode:              bit mask indicating the access mode (read, write,
+ * @open_mode:         bit mask indicating the access mode (read, write,
  *                     create)
  * @attributes:                attributes for newly created file
  * Returns:            handle to the opened file or NULL
  */
 static struct efi_file_handle *file_open(struct file_system *fs,
-               struct file_handle *parent, s16 *file_name, u64 mode,
+               struct file_handle *parent, u16 *file_name, u64 open_mode,
                u64 attributes)
 {
        struct file_handle *fh;
@@ -157,8 +182,8 @@ static struct efi_file_handle *file_open(struct file_system *fs,
        int flen = 0;
 
        if (file_name) {
-               utf16_to_utf8((u8 *)f0, (u16 *)file_name, 1);
-               flen = u16_strlen((u16 *)file_name);
+               utf16_to_utf8((u8 *)f0, file_name, 1);
+               flen = u16_strlen(file_name);
        }
 
        /* we could have a parent, but also an absolute path: */
@@ -171,11 +196,13 @@ static struct efi_file_handle *file_open(struct file_system *fs,
        /* +2 is for null and '/' */
        fh = calloc(1, sizeof(*fh) + plen + (flen * MAX_UTF8_PER_UTF16) + 2);
 
+       fh->open_mode = open_mode;
        fh->base = efi_file_handle_protocol;
        fh->fs = fs;
 
        if (parent) {
                char *p = fh->path;
+               int exists;
 
                if (plen > 0) {
                        strcpy(p, parent->path);
@@ -183,7 +210,7 @@ static struct efi_file_handle *file_open(struct file_system *fs,
                        *p++ = '/';
                }
 
-               utf16_to_utf8((u8 *)p, (u16 *)file_name, flen);
+               utf16_to_utf8((u8 *)p, file_name, flen);
 
                if (sanitize_path(fh->path))
                        goto error;
@@ -192,14 +219,19 @@ static struct efi_file_handle *file_open(struct file_system *fs,
                if (set_blk_dev(fh))
                        goto error;
 
-               if ((mode & EFI_FILE_MODE_CREATE) &&
-                   (attributes & EFI_FILE_DIRECTORY)) {
-                       if (fs_mkdir(fh->path))
-                               goto error;
-               } else if (!((mode & EFI_FILE_MODE_CREATE) ||
-                            fs_exists(fh->path)))
+               exists = fs_exists(fh->path);
+               /* fs_exists() calls fs_close(), so open file system again */
+               if (set_blk_dev(fh))
                        goto error;
 
+               if (!exists) {
+                       if (!(open_mode & EFI_FILE_MODE_CREATE) ||
+                           efi_create_file(fh, attributes))
+                               goto error;
+                       if (set_blk_dev(fh))
+                               goto error;
+               }
+
                /* figure out if file is a directory: */
                fh->isdir = is_dir(fh);
        } else {
@@ -216,13 +248,13 @@ error:
 
 static efi_status_t EFIAPI efi_file_open(struct efi_file_handle *file,
                struct efi_file_handle **new_handle,
-               s16 *file_name, u64 open_mode, u64 attributes)
+               u16 *file_name, u64 open_mode, u64 attributes)
 {
        struct file_handle *fh = to_fh(file);
        efi_status_t ret;
 
-       EFI_ENTRY("%p, %p, \"%ls\", %llx, %llu", file, new_handle, file_name,
-                 open_mode, attributes);
+       EFI_ENTRY("%p, %p, \"%ls\", %llx, %llu", file, new_handle,
+                 file_name, open_mode, attributes);
 
        /* Check parameters */
        if (!file || !new_handle || !file_name) {
@@ -253,10 +285,12 @@ static efi_status_t EFIAPI efi_file_open(struct efi_file_handle *file,
 
        /* Open file */
        *new_handle = file_open(fh->fs, fh, file_name, open_mode, attributes);
-       if (*new_handle)
+       if (*new_handle) {
+               EFI_PRINT("file handle %p\n", *new_handle);
                ret = EFI_SUCCESS;
-       else
+       } else {
                ret = EFI_NOT_FOUND;
+       }
 out:
        return EFI_EXIT(ret);
 }
@@ -282,24 +316,54 @@ static efi_status_t EFIAPI efi_file_delete(struct efi_file_handle *file)
 
        EFI_ENTRY("%p", file);
 
-       if (set_blk_dev(fh)) {
-               ret = EFI_DEVICE_ERROR;
-               goto error;
-       }
+       if (set_blk_dev(fh) || fs_unlink(fh->path))
+               ret = EFI_WARN_DELETE_FAILURE;
 
-       if (fs_unlink(fh->path))
-               ret = EFI_DEVICE_ERROR;
        file_close(fh);
-
-error:
        return EFI_EXIT(ret);
 }
 
+/**
+ * efi_get_file_size() - determine the size of a file
+ *
+ * @fh:                file handle
+ * @file_size: pointer to receive file size
+ * Return:     status code
+ */
+static efi_status_t efi_get_file_size(struct file_handle *fh,
+                                     loff_t *file_size)
+{
+       if (set_blk_dev(fh))
+               return EFI_DEVICE_ERROR;
+
+       if (fs_size(fh->path, file_size))
+               return EFI_DEVICE_ERROR;
+
+       return EFI_SUCCESS;
+}
+
 static efi_status_t file_read(struct file_handle *fh, u64 *buffer_size,
                void *buffer)
 {
        loff_t actread;
+       efi_status_t ret;
+       loff_t file_size;
+
+       if (!buffer) {
+               ret = EFI_INVALID_PARAMETER;
+               return ret;
+       }
+
+       ret = efi_get_file_size(fh, &file_size);
+       if (ret != EFI_SUCCESS)
+               return ret;
+       if (file_size < fh->offset) {
+               ret = EFI_DEVICE_ERROR;
+               return ret;
+       }
 
+       if (set_blk_dev(fh))
+               return EFI_DEVICE_ERROR;
        if (fs_read(fh->path, map_to_sysmem(buffer), fh->offset,
                    *buffer_size, &actread))
                return EFI_DEVICE_ERROR;
@@ -315,13 +379,18 @@ 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;
+       u64 required_size;
+       u16 *dst;
+
+       if (set_blk_dev(fh))
+               return EFI_DEVICE_ERROR;
 
        if (!fh->dirs) {
                assert(fh->offset == 0);
                fh->dirs = fs_opendir(fh->path);
                if (!fh->dirs)
                        return EFI_DEVICE_ERROR;
+               fh->dent = NULL;
        }
 
        /*
@@ -332,38 +401,27 @@ static efi_status_t dir_read(struct file_handle *fh, u64 *buffer_size,
         */
        if (fh->dent) {
                dent = fh->dent;
-               fh->dent = NULL;
        } else {
                dent = fs_readdir(fh->dirs);
        }
 
-
        if (!dent) {
-               /* no more files in directory: */
-               /* workaround shim.efi bug/quirk.. as find_boot_csv()
-                * loops through directory contents, it initially calls
-                * read w/ zero length buffer to find out how much mem
-                * to allocate for the EFI_FILE_INFO, then allocates,
-                * and then calls a 2nd time.  If we return size of
-                * zero the first time, it happily passes that to
-                * AllocateZeroPool(), and when that returns NULL it
-                * thinks it is EFI_OUT_OF_RESOURCES.  So on first
-                * call return a non-zero size:
-                */
-               if (*buffer_size == 0)
-                       *buffer_size = sizeof(*info);
-               else
-                       *buffer_size = 0;
+               /* no more files in directory */
+               *buffer_size = 0;
                return EFI_SUCCESS;
        }
 
        /* 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;
                return EFI_BUFFER_TOO_SMALL;
        }
+       if (!buffer)
+               return EFI_INVALID_PARAMETER;
+       fh->dent = NULL;
 
        *buffer_size = required_size;
        memset(info, 0, required_size);
@@ -375,7 +433,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((u16 *)info->file_name, dent->name);
+       dst = info->file_name;
+       utf8_utf16_strcpy(&dst, dent->name);
 
        fh->offset++;
 
@@ -391,16 +450,11 @@ static efi_status_t EFIAPI efi_file_read(struct efi_file_handle *file,
 
        EFI_ENTRY("%p, %p, %p", file, buffer_size, buffer);
 
-       if (!buffer_size || !buffer) {
+       if (!buffer_size) {
                ret = EFI_INVALID_PARAMETER;
                goto error;
        }
 
-       if (set_blk_dev(fh)) {
-               ret = EFI_DEVICE_ERROR;
-               goto error;
-       }
-
        bs = *buffer_size;
        if (fh->isdir)
                ret = dir_read(fh, &bs, buffer);
@@ -415,6 +469,19 @@ error:
        return EFI_EXIT(ret);
 }
 
+/**
+ * efi_file_write() - write to file
+ *
+ * This function implements the Write() service of the EFI_FILE_PROTOCOL.
+ *
+ * See the Unified Extensible Firmware Interface (UEFI) specification for
+ * details.
+ *
+ * @file:              file handle
+ * @buffer_size:       number of bytes to write
+ * @buffer:            buffer with the bytes to write
+ * Return:             status code
+ */
 static efi_status_t EFIAPI efi_file_write(struct efi_file_handle *file,
                                          efi_uintn_t *buffer_size,
                                          void *buffer)
@@ -425,21 +492,35 @@ static efi_status_t EFIAPI efi_file_write(struct efi_file_handle *file,
 
        EFI_ENTRY("%p, %p, %p", file, buffer_size, buffer);
 
+       if (!file || !buffer_size || !buffer) {
+               ret = EFI_INVALID_PARAMETER;
+               goto out;
+       }
+       if (fh->isdir) {
+               ret = EFI_UNSUPPORTED;
+               goto out;
+       }
+       if (!(fh->open_mode & EFI_FILE_MODE_WRITE)) {
+               ret = EFI_ACCESS_DENIED;
+               goto out;
+       }
+
+       if (!*buffer_size)
+               goto out;
+
        if (set_blk_dev(fh)) {
                ret = EFI_DEVICE_ERROR;
-               goto error;
+               goto out;
        }
-
        if (fs_write(fh->path, map_to_sysmem(buffer), fh->offset, *buffer_size,
                     &actwrite)) {
                ret = EFI_DEVICE_ERROR;
-               goto error;
+               goto out;
        }
-
        *buffer_size = actwrite;
        fh->offset += actwrite;
 
-error:
+out:
        return EFI_EXIT(ret);
 }
 
@@ -501,16 +582,9 @@ static efi_status_t EFIAPI efi_file_setpos(struct efi_file_handle *file,
        if (pos == ~0ULL) {
                loff_t file_size;
 
-               if (set_blk_dev(fh)) {
-                       ret = EFI_DEVICE_ERROR;
-                       goto error;
-               }
-
-               if (fs_size(fh->path, &file_size)) {
-                       ret = EFI_DEVICE_ERROR;
+               ret = efi_get_file_size(fh, &file_size);
+               if (ret != EFI_SUCCESS)
                        goto error;
-               }
-
                pos = file_size;
        }
 
@@ -527,9 +601,16 @@ 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);
 
+       if (!file || !info_type || !buffer_size ||
+           (*buffer_size && !buffer)) {
+               ret = EFI_INVALID_PARAMETER;
+               goto error;
+       }
+
        if (!guidcmp(info_type, &efi_file_info_guid)) {
                struct efi_file_info *info = buffer;
                char *filename = basename(fh);
@@ -537,22 +618,17 @@ 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;
                        goto error;
                }
 
-               if (set_blk_dev(fh)) {
-                       ret = EFI_DEVICE_ERROR;
+               ret = efi_get_file_size(fh, &file_size);
+               if (ret != EFI_SUCCESS)
                        goto error;
-               }
-
-               if (fs_size(fh->path, &file_size)) {
-                       ret = EFI_DEVICE_ERROR;
-                       goto error;
-               }
 
                memset(info, 0, required_size);
 
@@ -563,10 +639,11 @@ static efi_status_t EFIAPI efi_file_getinfo(struct efi_file_handle *file,
                if (fh->isdir)
                        info->attribute |= EFI_FILE_DIRECTORY;
 
-               ascii2unicode((u16 *)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;
+               struct disk_partition part;
                efi_uintn_t required_size;
                int r;
 
@@ -578,8 +655,7 @@ 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;
                if (*buffer_size < required_size) {
                        *buffer_size = required_size;
                        ret = EFI_BUFFER_TOO_SMALL;
@@ -589,16 +665,28 @@ static efi_status_t EFIAPI efi_file_getinfo(struct efi_file_handle *file,
                memset(info, 0, required_size);
 
                info->size = required_size;
-               info->read_only = true;
+               /*
+                * TODO: We cannot determine if the volume can be written to.
+                */
+               info->read_only = false;
                info->volume_size = part.size * part.blksz;
-               info->free_space = 0;
+               /*
+                * TODO: We currently have no function to determine the free
+                * space. The volume size is the best upper bound we have.
+                */
+               info->free_space = info->volume_size;
                info->block_size = part.blksz;
                /*
                 * 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);
+               info->volume_label[0] = 0;
+       } else if (!guidcmp(info_type, &efi_system_volume_label_id)) {
+               if (*buffer_size < 2) {
+                       *buffer_size = 2;
+                       ret = EFI_BUFFER_TOO_SMALL;
+                       goto error;
+               }
+               *(u16 *)buffer = 0;
        } else {
                ret = EFI_UNSUPPORTED;
        }
@@ -612,9 +700,65 @@ static efi_status_t EFIAPI efi_file_setinfo(struct efi_file_handle *file,
                                            efi_uintn_t buffer_size,
                                            void *buffer)
 {
-       EFI_ENTRY("%p, %p, %zu, %p", file, info_type, buffer_size, buffer);
+       struct file_handle *fh = to_fh(file);
+       efi_status_t ret = EFI_UNSUPPORTED;
 
-       return EFI_EXIT(EFI_UNSUPPORTED);
+       EFI_ENTRY("%p, %pUl, %zu, %p", file, info_type, buffer_size, buffer);
+
+       if (!guidcmp(info_type, &efi_file_info_guid)) {
+               struct efi_file_info *info = (struct efi_file_info *)buffer;
+               char *filename = basename(fh);
+               char *new_file_name, *pos;
+               loff_t file_size;
+
+               /* The buffer will always contain a file name. */
+               if (buffer_size < sizeof(struct efi_file_info) + 2 ||
+                   buffer_size < info->size) {
+                       ret = EFI_BAD_BUFFER_SIZE;
+                       goto out;
+               }
+               /* We cannot change the directory attribute */
+               if (!fh->isdir != !(info->attribute & EFI_FILE_DIRECTORY)) {
+                       ret = EFI_ACCESS_DENIED;
+                       goto out;
+               }
+               /* Check for renaming */
+               new_file_name = malloc(utf16_utf8_strlen(info->file_name) + 1);
+               if (!new_file_name) {
+                       ret = EFI_OUT_OF_RESOURCES;
+                       goto out;
+               }
+               pos = new_file_name;
+               utf16_utf8_strcpy(&pos, info->file_name);
+               if (strcmp(new_file_name, filename)) {
+                       /* TODO: we do not support renaming */
+                       EFI_PRINT("Renaming not supported\n");
+                       free(new_file_name);
+                       ret = EFI_ACCESS_DENIED;
+                       goto out;
+               }
+               free(new_file_name);
+               /* Check for truncation */
+               ret = efi_get_file_size(fh, &file_size);
+               if (ret != EFI_SUCCESS)
+                       goto out;
+               if (file_size != info->file_size) {
+                       /* TODO: we do not support truncation */
+                       EFI_PRINT("Truncation not supported\n");
+                       ret = EFI_ACCESS_DENIED;
+                       goto out;
+               }
+               /*
+                * We do not care for the other attributes
+                * TODO: Support read only
+                */
+               ret = EFI_SUCCESS;
+       } else {
+               /* TODO: We do not support changing the volume label */
+               ret = EFI_UNSUPPORTED;
+       }
+out:
+       return EFI_EXIT(ret);
 }
 
 static efi_status_t EFIAPI efi_file_flush(struct efi_file_handle *file)
@@ -623,8 +767,34 @@ static efi_status_t EFIAPI efi_file_flush(struct efi_file_handle *file)
        return EFI_EXIT(EFI_SUCCESS);
 }
 
+static efi_status_t EFIAPI efi_file_open_ex(struct efi_file_handle *file,
+                       struct efi_file_handle **new_handle,
+                       u16 *file_name, u64 open_mode, u64 attributes,
+                       struct efi_file_io_token *token)
+{
+       return EFI_UNSUPPORTED;
+}
+
+static efi_status_t EFIAPI efi_file_read_ex(struct efi_file_handle *file,
+                       struct efi_file_io_token *token)
+{
+       return EFI_UNSUPPORTED;
+}
+
+static efi_status_t EFIAPI efi_file_write_ex(struct efi_file_handle *file,
+                       struct efi_file_io_token *token)
+{
+       return EFI_UNSUPPORTED;
+}
+
+static efi_status_t EFIAPI efi_file_flush_ex(struct efi_file_handle *file,
+                       struct efi_file_io_token *token)
+{
+       return EFI_UNSUPPORTED;
+}
+
 static const struct efi_file_handle efi_file_handle_protocol = {
-       .rev = EFI_FILE_PROTOCOL_REVISION,
+       .rev = EFI_FILE_PROTOCOL_REVISION2,
        .open = efi_file_open,
        .close = efi_file_close,
        .delete = efi_file_delete,
@@ -635,8 +805,18 @@ static const struct efi_file_handle efi_file_handle_protocol = {
        .getinfo = efi_file_getinfo,
        .setinfo = efi_file_setinfo,
        .flush = efi_file_flush,
+       .open_ex = efi_file_open_ex,
+       .read_ex = efi_file_read_ex,
+       .write_ex = efi_file_write_ex,
+       .flush_ex = efi_file_flush_ex,
 };
 
+/**
+ * efi_file_from_path() - open file via device path
+ *
+ * @fp:                device path
+ * @return:    EFI_FILE_PROTOCOL for the file or NULL
+ */
 struct efi_file_handle *efi_file_from_path(struct efi_device_path *fp)
 {
        struct efi_simple_file_system_protocol *v;
@@ -651,14 +831,19 @@ struct efi_file_handle *efi_file_from_path(struct efi_device_path *fp)
        if (ret != EFI_SUCCESS)
                return NULL;
 
-       /* skip over device-path nodes before the file path: */
+       /* Skip over device-path nodes before the file path. */
        while (fp && !EFI_DP_TYPE(fp, MEDIA_DEVICE, FILE_PATH))
                fp = efi_dp_next(fp);
 
+       /*
+        * Step through the nodes of the directory path until the actual file
+        * node is reached which is the final node in the device path.
+        */
        while (fp) {
                struct efi_device_path_file_path *fdp =
                        container_of(fp, struct efi_device_path_file_path, dp);
                struct efi_file_handle *f2;
+               u16 *filename;
 
                if (!EFI_DP_TYPE(fp, MEDIA_DEVICE, FILE_PATH)) {
                        printf("bad file path!\n");
@@ -666,8 +851,12 @@ struct efi_file_handle *efi_file_from_path(struct efi_device_path *fp)
                        return NULL;
                }
 
-               EFI_CALL(ret = f->open(f, &f2, (s16 *)fdp->str,
+               filename = u16_strdup(fdp->str);
+               if (!filename)
+                       return NULL;
+               EFI_CALL(ret = f->open(f, &f2, filename,
                                       EFI_FILE_MODE_READ, 0));
+               free(filename);
                if (ret != EFI_SUCCESS)
                        return NULL;