2018-05-07 21:02:21 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0+
|
2017-09-13 22:05:34 +00:00
|
|
|
/*
|
2019-09-06 19:37:21 +00:00
|
|
|
* EFI_FILE_PROTOCOL
|
2017-09-13 22:05:34 +00:00
|
|
|
*
|
2019-09-06 19:37:21 +00:00
|
|
|
* Copyright (c) 2017 Rob Clark
|
2017-09-13 22:05:34 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <common.h>
|
|
|
|
#include <charset.h>
|
|
|
|
#include <efi_loader.h>
|
2020-05-10 17:40:05 +00:00
|
|
|
#include <log.h>
|
2017-09-13 22:05:34 +00:00
|
|
|
#include <malloc.h>
|
2018-08-08 09:54:32 +00:00
|
|
|
#include <mapmem.h>
|
2017-09-13 22:05:34 +00:00
|
|
|
#include <fs.h>
|
2020-05-10 17:39:58 +00:00
|
|
|
#include <part.h>
|
2017-09-13 22:05:34 +00:00
|
|
|
|
2018-04-04 13:42:11 +00:00
|
|
|
/* GUID for file system information */
|
|
|
|
const efi_guid_t efi_file_system_info_guid = EFI_FILE_SYSTEM_INFO_GUID;
|
|
|
|
|
2019-09-08 08:32:54 +00:00
|
|
|
/* GUID to obtain the volume label */
|
|
|
|
const efi_guid_t efi_system_volume_label_id = EFI_FILE_SYSTEM_VOLUME_LABEL_ID;
|
|
|
|
|
2017-09-13 22:05:34 +00:00
|
|
|
struct file_system {
|
|
|
|
struct efi_simple_file_system_protocol base;
|
|
|
|
struct efi_device_path *dp;
|
|
|
|
struct blk_desc *desc;
|
|
|
|
int part;
|
|
|
|
};
|
|
|
|
#define to_fs(x) container_of(x, struct file_system, base)
|
|
|
|
|
|
|
|
struct file_handle {
|
|
|
|
struct efi_file_handle base;
|
|
|
|
struct file_system *fs;
|
|
|
|
loff_t offset; /* current file position/cursor */
|
|
|
|
int isdir;
|
2019-09-06 19:37:21 +00:00
|
|
|
u64 open_mode;
|
2017-09-13 22:05:34 +00:00
|
|
|
|
|
|
|
/* for reading a directory: */
|
|
|
|
struct fs_dir_stream *dirs;
|
|
|
|
struct fs_dirent *dent;
|
|
|
|
|
|
|
|
char path[0];
|
|
|
|
};
|
|
|
|
#define to_fh(x) container_of(x, struct file_handle, base)
|
|
|
|
|
|
|
|
static const struct efi_file_handle efi_file_handle_protocol;
|
|
|
|
|
|
|
|
static char *basename(struct file_handle *fh)
|
|
|
|
{
|
|
|
|
char *s = strrchr(fh->path, '/');
|
|
|
|
if (s)
|
|
|
|
return s + 1;
|
|
|
|
return fh->path;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int set_blk_dev(struct file_handle *fh)
|
|
|
|
{
|
|
|
|
return fs_set_blk_dev_with_part(fh->fs->desc, fh->fs->part);
|
|
|
|
}
|
|
|
|
|
2018-10-02 03:57:32 +00:00
|
|
|
/**
|
|
|
|
* is_dir() - check if file handle points to directory
|
|
|
|
*
|
|
|
|
* We assume that set_blk_dev(fh) has been called already.
|
|
|
|
*
|
|
|
|
* @fh: file handle
|
|
|
|
* Return: true if file handle points to a directory
|
|
|
|
*/
|
2017-09-13 22:05:34 +00:00
|
|
|
static int is_dir(struct file_handle *fh)
|
|
|
|
{
|
|
|
|
struct fs_dir_stream *dirs;
|
|
|
|
|
|
|
|
dirs = fs_opendir(fh->path);
|
|
|
|
if (!dirs)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
fs_closedir(dirs);
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Normalize a path which may include either back or fwd slashes,
|
|
|
|
* double slashes, . or .. entries in the path, etc.
|
|
|
|
*/
|
|
|
|
static int sanitize_path(char *path)
|
|
|
|
{
|
|
|
|
char *p;
|
|
|
|
|
|
|
|
/* backslash to slash: */
|
|
|
|
p = path;
|
|
|
|
while ((p = strchr(p, '\\')))
|
|
|
|
*p++ = '/';
|
|
|
|
|
|
|
|
/* handle double-slashes: */
|
|
|
|
p = path;
|
|
|
|
while ((p = strstr(p, "//"))) {
|
|
|
|
char *src = p + 1;
|
|
|
|
memmove(p, src, strlen(src) + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* handle extra /.'s */
|
|
|
|
p = path;
|
|
|
|
while ((p = strstr(p, "/."))) {
|
|
|
|
/*
|
|
|
|
* You'd be tempted to do this *after* handling ".."s
|
|
|
|
* below to avoid having to check if "/." is start of
|
|
|
|
* a "/..", but that won't have the correct results..
|
|
|
|
* for example, "/foo/./../bar" would get resolved to
|
|
|
|
* "/foo/bar" if you did these two passes in the other
|
|
|
|
* order
|
|
|
|
*/
|
|
|
|
if (p[2] == '.') {
|
|
|
|
p += 2;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
char *src = p + 2;
|
|
|
|
memmove(p, src, strlen(src) + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* handle extra /..'s: */
|
|
|
|
p = path;
|
|
|
|
while ((p = strstr(p, "/.."))) {
|
|
|
|
char *src = p + 3;
|
|
|
|
|
|
|
|
p--;
|
|
|
|
|
|
|
|
/* find beginning of previous path entry: */
|
|
|
|
while (true) {
|
|
|
|
if (p < path)
|
|
|
|
return -1;
|
|
|
|
if (*p == '/')
|
|
|
|
break;
|
|
|
|
p--;
|
|
|
|
}
|
|
|
|
|
|
|
|
memmove(p, src, strlen(src) + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-04-06 14:27:34 +00:00
|
|
|
/**
|
|
|
|
* 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);
|
|
|
|
}
|
|
|
|
|
2018-09-12 17:00:02 +00:00
|
|
|
/**
|
|
|
|
* file_open() - open a file handle
|
|
|
|
*
|
|
|
|
* @fs: file system
|
|
|
|
* @parent: directory relative to which the file is to be opened
|
|
|
|
* @file_name: path of the file to be opened. '\', '.', or '..' may
|
|
|
|
* be used as modifiers. A leading backslash indicates an
|
|
|
|
* absolute path.
|
2019-09-06 19:37:21 +00:00
|
|
|
* @open_mode: bit mask indicating the access mode (read, write,
|
2018-09-12 17:00:02 +00:00
|
|
|
* create)
|
|
|
|
* @attributes: attributes for newly created file
|
|
|
|
* Returns: handle to the opened file or NULL
|
2017-09-13 22:05:34 +00:00
|
|
|
*/
|
|
|
|
static struct efi_file_handle *file_open(struct file_system *fs,
|
2019-09-06 19:37:21 +00:00
|
|
|
struct file_handle *parent, u16 *file_name, u64 open_mode,
|
2018-09-11 06:59:12 +00:00
|
|
|
u64 attributes)
|
2017-09-13 22:05:34 +00:00
|
|
|
{
|
|
|
|
struct file_handle *fh;
|
|
|
|
char f0[MAX_UTF8_PER_UTF16] = {0};
|
|
|
|
int plen = 0;
|
|
|
|
int flen = 0;
|
|
|
|
|
|
|
|
if (file_name) {
|
2019-01-12 11:02:33 +00:00
|
|
|
utf16_to_utf8((u8 *)f0, file_name, 1);
|
|
|
|
flen = u16_strlen(file_name);
|
2017-09-13 22:05:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* we could have a parent, but also an absolute path: */
|
|
|
|
if (f0[0] == '\\') {
|
|
|
|
plen = 0;
|
|
|
|
} else if (parent) {
|
|
|
|
plen = strlen(parent->path) + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* +2 is for null and '/' */
|
|
|
|
fh = calloc(1, sizeof(*fh) + plen + (flen * MAX_UTF8_PER_UTF16) + 2);
|
|
|
|
|
2019-09-06 19:37:21 +00:00
|
|
|
fh->open_mode = open_mode;
|
2017-09-13 22:05:34 +00:00
|
|
|
fh->base = efi_file_handle_protocol;
|
|
|
|
fh->fs = fs;
|
|
|
|
|
|
|
|
if (parent) {
|
|
|
|
char *p = fh->path;
|
2019-04-06 14:27:34 +00:00
|
|
|
int exists;
|
2017-09-13 22:05:34 +00:00
|
|
|
|
|
|
|
if (plen > 0) {
|
|
|
|
strcpy(p, parent->path);
|
|
|
|
p += plen - 1;
|
|
|
|
*p++ = '/';
|
|
|
|
}
|
|
|
|
|
2019-01-12 11:02:33 +00:00
|
|
|
utf16_to_utf8((u8 *)p, file_name, flen);
|
2017-09-13 22:05:34 +00:00
|
|
|
|
|
|
|
if (sanitize_path(fh->path))
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
/* check if file exists: */
|
|
|
|
if (set_blk_dev(fh))
|
|
|
|
goto error;
|
|
|
|
|
2019-04-06 14:27:34 +00:00
|
|
|
exists = fs_exists(fh->path);
|
2019-02-09 21:23:48 +00:00
|
|
|
/* fs_exists() calls fs_close(), so open file system again */
|
|
|
|
if (set_blk_dev(fh))
|
|
|
|
goto error;
|
|
|
|
|
2019-04-06 14:27:34 +00:00
|
|
|
if (!exists) {
|
2019-09-06 19:37:21 +00:00
|
|
|
if (!(open_mode & EFI_FILE_MODE_CREATE) ||
|
2019-04-06 14:27:34 +00:00
|
|
|
efi_create_file(fh, attributes))
|
|
|
|
goto error;
|
2019-09-06 19:37:21 +00:00
|
|
|
if (set_blk_dev(fh))
|
|
|
|
goto error;
|
2019-04-06 14:27:34 +00:00
|
|
|
}
|
|
|
|
|
2017-09-13 22:05:34 +00:00
|
|
|
/* figure out if file is a directory: */
|
|
|
|
fh->isdir = is_dir(fh);
|
|
|
|
} else {
|
|
|
|
fh->isdir = 1;
|
|
|
|
strcpy(fh->path, "");
|
|
|
|
}
|
|
|
|
|
|
|
|
return &fh->base;
|
|
|
|
|
|
|
|
error:
|
|
|
|
free(fh);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2021-01-01 07:39:43 +00:00
|
|
|
static efi_status_t efi_file_open_int(struct efi_file_handle *this,
|
|
|
|
struct efi_file_handle **new_handle,
|
|
|
|
u16 *file_name, u64 open_mode,
|
|
|
|
u64 attributes)
|
2017-09-13 22:05:34 +00:00
|
|
|
{
|
2021-01-01 07:39:43 +00:00
|
|
|
struct file_handle *fh = to_fh(this);
|
2018-09-12 16:43:58 +00:00
|
|
|
efi_status_t ret;
|
2017-09-13 22:05:34 +00:00
|
|
|
|
2018-09-12 16:43:58 +00:00
|
|
|
/* Check parameters */
|
2021-01-01 07:39:43 +00:00
|
|
|
if (!this || !new_handle || !file_name) {
|
2018-09-12 16:43:58 +00:00
|
|
|
ret = EFI_INVALID_PARAMETER;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
if (open_mode != EFI_FILE_MODE_READ &&
|
|
|
|
open_mode != (EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE) &&
|
|
|
|
open_mode != (EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE |
|
|
|
|
EFI_FILE_MODE_CREATE)) {
|
|
|
|
ret = EFI_INVALID_PARAMETER;
|
|
|
|
goto out;
|
|
|
|
}
|
2018-09-13 19:31:49 +00:00
|
|
|
/*
|
|
|
|
* The UEFI spec requires that attributes are only set in create mode.
|
|
|
|
* The SCT does not care about this and sets EFI_FILE_DIRECTORY in
|
|
|
|
* read mode. EDK2 does not check that attributes are zero if not in
|
|
|
|
* create mode.
|
|
|
|
*
|
|
|
|
* So here we only check attributes in create mode and do not check
|
|
|
|
* that they are zero otherwise.
|
|
|
|
*/
|
|
|
|
if ((open_mode & EFI_FILE_MODE_CREATE) &&
|
2018-09-12 16:43:58 +00:00
|
|
|
(attributes & (EFI_FILE_READ_ONLY | ~EFI_FILE_VALID_ATTR))) {
|
|
|
|
ret = EFI_INVALID_PARAMETER;
|
|
|
|
goto out;
|
|
|
|
}
|
2017-09-13 22:05:34 +00:00
|
|
|
|
2018-09-12 16:43:58 +00:00
|
|
|
/* Open file */
|
|
|
|
*new_handle = file_open(fh->fs, fh, file_name, open_mode, attributes);
|
2019-04-06 14:36:16 +00:00
|
|
|
if (*new_handle) {
|
|
|
|
EFI_PRINT("file handle %p\n", *new_handle);
|
2018-09-12 16:43:58 +00:00
|
|
|
ret = EFI_SUCCESS;
|
2019-04-06 14:36:16 +00:00
|
|
|
} else {
|
2018-09-12 16:43:58 +00:00
|
|
|
ret = EFI_NOT_FOUND;
|
2019-04-06 14:36:16 +00:00
|
|
|
}
|
2021-01-01 07:39:43 +00:00
|
|
|
out:
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* efi_file_open_()
|
|
|
|
*
|
|
|
|
* This function implements the Open service of the File Protocol.
|
|
|
|
* See the UEFI spec for details.
|
|
|
|
*
|
|
|
|
* @this: EFI_FILE_PROTOCOL instance
|
|
|
|
* @new_handle: on return pointer to file handle
|
|
|
|
* @file_name: file name
|
|
|
|
* @open_mode: mode to open the file (read, read/write, create/read/write)
|
|
|
|
* @attributes: attributes for newly created file
|
|
|
|
*/
|
|
|
|
static efi_status_t EFIAPI efi_file_open(struct efi_file_handle *this,
|
|
|
|
struct efi_file_handle **new_handle,
|
|
|
|
u16 *file_name, u64 open_mode,
|
|
|
|
u64 attributes)
|
|
|
|
{
|
|
|
|
efi_status_t ret;
|
|
|
|
|
|
|
|
EFI_ENTRY("%p, %p, \"%ls\", %llx, %llu", this, new_handle,
|
|
|
|
file_name, open_mode, attributes);
|
|
|
|
|
|
|
|
ret = efi_file_open_int(this, new_handle, file_name, open_mode,
|
|
|
|
attributes);
|
|
|
|
|
|
|
|
return EFI_EXIT(ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* efi_file_open_ex() - open file asynchronously
|
|
|
|
*
|
|
|
|
* This function implements the OpenEx service of the File Protocol.
|
|
|
|
* See the UEFI spec for details.
|
|
|
|
*
|
|
|
|
* @this: EFI_FILE_PROTOCOL instance
|
|
|
|
* @new_handle: on return pointer to file handle
|
|
|
|
* @file_name: file name
|
|
|
|
* @open_mode: mode to open the file (read, read/write, create/read/write)
|
|
|
|
* @attributes: attributes for newly created file
|
|
|
|
* @token: transaction token
|
|
|
|
*/
|
|
|
|
static efi_status_t EFIAPI efi_file_open_ex(struct efi_file_handle *this,
|
|
|
|
struct efi_file_handle **new_handle,
|
|
|
|
u16 *file_name, u64 open_mode,
|
|
|
|
u64 attributes,
|
|
|
|
struct efi_file_io_token *token)
|
|
|
|
{
|
|
|
|
efi_status_t ret;
|
|
|
|
|
|
|
|
EFI_ENTRY("%p, %p, \"%ls\", %llx, %llu, %p", this, new_handle,
|
|
|
|
file_name, open_mode, attributes, token);
|
|
|
|
|
|
|
|
if (!token) {
|
|
|
|
ret = EFI_INVALID_PARAMETER;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = efi_file_open_int(this, new_handle, file_name, open_mode,
|
|
|
|
attributes);
|
|
|
|
|
|
|
|
if (ret == EFI_SUCCESS && token->event) {
|
|
|
|
token->status = EFI_SUCCESS;
|
|
|
|
efi_signal_event(token->event);
|
|
|
|
}
|
|
|
|
|
2018-09-12 16:43:58 +00:00
|
|
|
out:
|
|
|
|
return EFI_EXIT(ret);
|
2017-09-13 22:05:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static efi_status_t file_close(struct file_handle *fh)
|
|
|
|
{
|
|
|
|
fs_closedir(fh->dirs);
|
|
|
|
free(fh);
|
|
|
|
return EFI_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
static efi_status_t EFIAPI efi_file_close(struct efi_file_handle *file)
|
|
|
|
{
|
|
|
|
struct file_handle *fh = to_fh(file);
|
|
|
|
EFI_ENTRY("%p", file);
|
|
|
|
return EFI_EXIT(file_close(fh));
|
|
|
|
}
|
|
|
|
|
|
|
|
static efi_status_t EFIAPI efi_file_delete(struct efi_file_handle *file)
|
|
|
|
{
|
|
|
|
struct file_handle *fh = to_fh(file);
|
2018-09-11 06:59:16 +00:00
|
|
|
efi_status_t ret = EFI_SUCCESS;
|
|
|
|
|
2017-09-13 22:05:34 +00:00
|
|
|
EFI_ENTRY("%p", file);
|
2018-09-11 06:59:16 +00:00
|
|
|
|
2019-06-17 20:00:13 +00:00
|
|
|
if (set_blk_dev(fh) || fs_unlink(fh->path))
|
|
|
|
ret = EFI_WARN_DELETE_FAILURE;
|
2018-09-11 06:59:16 +00:00
|
|
|
|
2017-09-13 22:05:34 +00:00
|
|
|
file_close(fh);
|
2018-09-11 06:59:16 +00:00
|
|
|
return EFI_EXIT(ret);
|
2017-09-13 22:05:34 +00:00
|
|
|
}
|
|
|
|
|
2019-09-07 21:28:04 +00:00
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
}
|
|
|
|
|
2021-03-17 19:54:59 +00:00
|
|
|
/**
|
|
|
|
* efi_file_size() - Get the size of a file using an EFI file handle
|
|
|
|
*
|
|
|
|
* @fh: EFI file handle
|
|
|
|
* @size: buffer to fill in the discovered size
|
|
|
|
*
|
|
|
|
* Return: size of the file
|
|
|
|
*/
|
|
|
|
efi_status_t efi_file_size(struct efi_file_handle *fh, efi_uintn_t *size)
|
|
|
|
{
|
|
|
|
struct efi_file_info *info = NULL;
|
|
|
|
efi_uintn_t bs = 0;
|
|
|
|
efi_status_t ret;
|
|
|
|
|
|
|
|
*size = 0;
|
|
|
|
ret = EFI_CALL(fh->getinfo(fh, (efi_guid_t *)&efi_file_info_guid, &bs,
|
|
|
|
info));
|
|
|
|
if (ret != EFI_BUFFER_TOO_SMALL) {
|
|
|
|
ret = EFI_DEVICE_ERROR;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
info = malloc(bs);
|
|
|
|
if (!info) {
|
|
|
|
ret = EFI_OUT_OF_RESOURCES;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
ret = EFI_CALL(fh->getinfo(fh, (efi_guid_t *)&efi_file_info_guid, &bs,
|
|
|
|
info));
|
|
|
|
if (ret != EFI_SUCCESS)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
*size = info->file_size;
|
|
|
|
|
|
|
|
out:
|
|
|
|
free(info);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-09-13 22:05:34 +00:00
|
|
|
static efi_status_t file_read(struct file_handle *fh, u64 *buffer_size,
|
|
|
|
void *buffer)
|
|
|
|
{
|
|
|
|
loff_t actread;
|
2019-09-07 21:28:04 +00:00
|
|
|
efi_status_t ret;
|
|
|
|
loff_t file_size;
|
|
|
|
|
2020-07-22 07:43:31 +00:00
|
|
|
if (!buffer) {
|
|
|
|
ret = EFI_INVALID_PARAMETER;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-09-07 21:28:04 +00:00
|
|
|
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;
|
|
|
|
}
|
2017-09-13 22:05:34 +00:00
|
|
|
|
2019-09-07 21:28:04 +00:00
|
|
|
if (set_blk_dev(fh))
|
|
|
|
return EFI_DEVICE_ERROR;
|
2018-09-15 06:51:00 +00:00
|
|
|
if (fs_read(fh->path, map_to_sysmem(buffer), fh->offset,
|
2017-09-13 22:05:34 +00:00
|
|
|
*buffer_size, &actread))
|
|
|
|
return EFI_DEVICE_ERROR;
|
|
|
|
|
|
|
|
*buffer_size = actread;
|
|
|
|
fh->offset += actread;
|
|
|
|
|
|
|
|
return EFI_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
static efi_status_t dir_read(struct file_handle *fh, u64 *buffer_size,
|
|
|
|
void *buffer)
|
|
|
|
{
|
|
|
|
struct efi_file_info *info = buffer;
|
|
|
|
struct fs_dirent *dent;
|
2019-09-07 20:34:07 +00:00
|
|
|
u64 required_size;
|
2019-09-07 19:05:45 +00:00
|
|
|
u16 *dst;
|
2017-09-13 22:05:34 +00:00
|
|
|
|
2019-09-07 21:28:04 +00:00
|
|
|
if (set_blk_dev(fh))
|
|
|
|
return EFI_DEVICE_ERROR;
|
|
|
|
|
2017-09-13 22:05:34 +00:00
|
|
|
if (!fh->dirs) {
|
|
|
|
assert(fh->offset == 0);
|
|
|
|
fh->dirs = fs_opendir(fh->path);
|
|
|
|
if (!fh->dirs)
|
|
|
|
return EFI_DEVICE_ERROR;
|
2019-09-07 20:34:07 +00:00
|
|
|
fh->dent = NULL;
|
2017-09-13 22:05:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* So this is a bit awkward. Since fs layer is stateful and we
|
|
|
|
* can't rewind an entry, in the EFI_BUFFER_TOO_SMALL case below
|
|
|
|
* we might have to return without consuming the dent.. so we
|
|
|
|
* have to stash it for next call.
|
|
|
|
*/
|
|
|
|
if (fh->dent) {
|
|
|
|
dent = fh->dent;
|
|
|
|
} else {
|
|
|
|
dent = fs_readdir(fh->dirs);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!dent) {
|
2019-09-07 20:34:07 +00:00
|
|
|
/* no more files in directory */
|
|
|
|
*buffer_size = 0;
|
2017-09-13 22:05:34 +00:00
|
|
|
return EFI_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* check buffer size: */
|
2019-09-07 19:05:45 +00:00
|
|
|
required_size = sizeof(*info) +
|
|
|
|
2 * (utf8_utf16_strlen(dent->name) + 1);
|
2017-09-13 22:05:34 +00:00
|
|
|
if (*buffer_size < required_size) {
|
|
|
|
*buffer_size = required_size;
|
|
|
|
fh->dent = dent;
|
|
|
|
return EFI_BUFFER_TOO_SMALL;
|
|
|
|
}
|
2020-07-22 07:43:31 +00:00
|
|
|
if (!buffer)
|
|
|
|
return EFI_INVALID_PARAMETER;
|
2019-09-07 20:34:07 +00:00
|
|
|
fh->dent = NULL;
|
2017-09-13 22:05:34 +00:00
|
|
|
|
|
|
|
*buffer_size = required_size;
|
|
|
|
memset(info, 0, required_size);
|
|
|
|
|
|
|
|
info->size = required_size;
|
|
|
|
info->file_size = dent->size;
|
|
|
|
info->physical_size = dent->size;
|
|
|
|
|
|
|
|
if (dent->type == FS_DT_DIR)
|
|
|
|
info->attribute |= EFI_FILE_DIRECTORY;
|
|
|
|
|
2019-09-07 19:05:45 +00:00
|
|
|
dst = info->file_name;
|
|
|
|
utf8_utf16_strcpy(&dst, dent->name);
|
2017-09-13 22:05:34 +00:00
|
|
|
|
|
|
|
fh->offset++;
|
|
|
|
|
|
|
|
return EFI_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2021-01-01 07:39:43 +00:00
|
|
|
static efi_status_t efi_file_read_int(struct efi_file_handle *this,
|
|
|
|
efi_uintn_t *buffer_size, void *buffer)
|
2017-09-13 22:05:34 +00:00
|
|
|
{
|
2021-01-01 07:39:43 +00:00
|
|
|
struct file_handle *fh = to_fh(this);
|
2017-09-13 22:05:34 +00:00
|
|
|
efi_status_t ret = EFI_SUCCESS;
|
2018-04-03 20:37:11 +00:00
|
|
|
u64 bs;
|
2017-09-13 22:05:34 +00:00
|
|
|
|
2021-04-28 13:54:01 +00:00
|
|
|
if (!this || !buffer_size)
|
2021-01-01 07:39:43 +00:00
|
|
|
return EFI_INVALID_PARAMETER;
|
2018-04-03 20:37:11 +00:00
|
|
|
|
|
|
|
bs = *buffer_size;
|
2017-09-13 22:05:34 +00:00
|
|
|
if (fh->isdir)
|
2018-04-03 20:37:11 +00:00
|
|
|
ret = dir_read(fh, &bs, buffer);
|
2017-09-13 22:05:34 +00:00
|
|
|
else
|
2018-04-03 20:37:11 +00:00
|
|
|
ret = file_read(fh, &bs, buffer);
|
|
|
|
if (bs <= SIZE_MAX)
|
|
|
|
*buffer_size = bs;
|
|
|
|
else
|
|
|
|
*buffer_size = SIZE_MAX;
|
2017-09-13 22:05:34 +00:00
|
|
|
|
2021-01-01 07:39:43 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* efi_file_read() - read file
|
|
|
|
*
|
|
|
|
* This function implements the Read() service of the EFI_FILE_PROTOCOL.
|
|
|
|
*
|
|
|
|
* See the Unified Extensible Firmware Interface (UEFI) specification for
|
|
|
|
* details.
|
|
|
|
*
|
|
|
|
* @this: file protocol instance
|
|
|
|
* @buffer_size: number of bytes to read
|
|
|
|
* @buffer: read buffer
|
|
|
|
* Return: status code
|
|
|
|
*/
|
|
|
|
static efi_status_t EFIAPI efi_file_read(struct efi_file_handle *this,
|
|
|
|
efi_uintn_t *buffer_size, void *buffer)
|
|
|
|
{
|
|
|
|
efi_status_t ret;
|
|
|
|
|
|
|
|
EFI_ENTRY("%p, %p, %p", this, buffer_size, buffer);
|
|
|
|
|
|
|
|
ret = efi_file_read_int(this, buffer_size, buffer);
|
|
|
|
|
2017-09-13 22:05:34 +00:00
|
|
|
return EFI_EXIT(ret);
|
|
|
|
}
|
|
|
|
|
2019-09-06 19:37:21 +00:00
|
|
|
/**
|
2021-01-01 07:39:43 +00:00
|
|
|
* efi_file_read_ex() - read file asynchonously
|
2019-09-06 19:37:21 +00:00
|
|
|
*
|
2021-01-01 07:39:43 +00:00
|
|
|
* This function implements the ReadEx() service of the EFI_FILE_PROTOCOL.
|
2019-09-06 19:37:21 +00:00
|
|
|
*
|
|
|
|
* See the Unified Extensible Firmware Interface (UEFI) specification for
|
|
|
|
* details.
|
|
|
|
*
|
2021-01-01 07:39:43 +00:00
|
|
|
* @this: file protocol instance
|
|
|
|
* @token: transaction token
|
2019-09-06 19:37:21 +00:00
|
|
|
* Return: status code
|
|
|
|
*/
|
2021-01-01 07:39:43 +00:00
|
|
|
static efi_status_t EFIAPI efi_file_read_ex(struct efi_file_handle *this,
|
|
|
|
struct efi_file_io_token *token)
|
2017-09-13 22:05:34 +00:00
|
|
|
{
|
2021-01-01 07:39:43 +00:00
|
|
|
efi_status_t ret;
|
|
|
|
|
|
|
|
EFI_ENTRY("%p, %p", this, token);
|
|
|
|
|
|
|
|
if (!token) {
|
|
|
|
ret = EFI_INVALID_PARAMETER;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = efi_file_read_int(this, &token->buffer_size, token->buffer);
|
|
|
|
|
|
|
|
if (ret == EFI_SUCCESS && token->event) {
|
|
|
|
token->status = EFI_SUCCESS;
|
|
|
|
efi_signal_event(token->event);
|
|
|
|
}
|
|
|
|
|
|
|
|
out:
|
|
|
|
return EFI_EXIT(ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
static efi_status_t efi_file_write_int(struct efi_file_handle *this,
|
|
|
|
efi_uintn_t *buffer_size, void *buffer)
|
|
|
|
{
|
|
|
|
struct file_handle *fh = to_fh(this);
|
2017-09-13 22:05:34 +00:00
|
|
|
efi_status_t ret = EFI_SUCCESS;
|
|
|
|
loff_t actwrite;
|
|
|
|
|
2021-01-01 07:39:43 +00:00
|
|
|
if (!this || !buffer_size || !buffer) {
|
2019-09-06 19:37:21 +00:00
|
|
|
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;
|
|
|
|
|
2017-09-13 22:05:34 +00:00
|
|
|
if (set_blk_dev(fh)) {
|
|
|
|
ret = EFI_DEVICE_ERROR;
|
2019-09-06 19:37:21 +00:00
|
|
|
goto out;
|
2017-09-13 22:05:34 +00:00
|
|
|
}
|
2018-09-15 06:51:00 +00:00
|
|
|
if (fs_write(fh->path, map_to_sysmem(buffer), fh->offset, *buffer_size,
|
2017-09-13 22:05:34 +00:00
|
|
|
&actwrite)) {
|
|
|
|
ret = EFI_DEVICE_ERROR;
|
2019-09-06 19:37:21 +00:00
|
|
|
goto out;
|
2017-09-13 22:05:34 +00:00
|
|
|
}
|
|
|
|
*buffer_size = actwrite;
|
|
|
|
fh->offset += actwrite;
|
|
|
|
|
2021-01-01 07:39:43 +00:00
|
|
|
out:
|
|
|
|
return 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.
|
|
|
|
*
|
|
|
|
* @this: file protocol instance
|
|
|
|
* @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 *this,
|
|
|
|
efi_uintn_t *buffer_size,
|
|
|
|
void *buffer)
|
|
|
|
{
|
|
|
|
efi_status_t ret;
|
|
|
|
|
|
|
|
EFI_ENTRY("%p, %p, %p", this, buffer_size, buffer);
|
|
|
|
|
|
|
|
ret = efi_file_write_int(this, buffer_size, buffer);
|
|
|
|
|
|
|
|
return EFI_EXIT(ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* efi_file_write_ex() - write to file
|
|
|
|
*
|
|
|
|
* This function implements the WriteEx() service of the EFI_FILE_PROTOCOL.
|
|
|
|
*
|
|
|
|
* See the Unified Extensible Firmware Interface (UEFI) specification for
|
|
|
|
* details.
|
|
|
|
*
|
|
|
|
* @this: file protocol instance
|
|
|
|
* @token: transaction token
|
|
|
|
* Return: status code
|
|
|
|
*/
|
|
|
|
static efi_status_t EFIAPI efi_file_write_ex(struct efi_file_handle *this,
|
|
|
|
struct efi_file_io_token *token)
|
|
|
|
{
|
|
|
|
efi_status_t ret;
|
|
|
|
|
|
|
|
EFI_ENTRY("%p, %p", this, token);
|
|
|
|
|
|
|
|
if (!token) {
|
|
|
|
ret = EFI_INVALID_PARAMETER;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = efi_file_write_int(this, &token->buffer_size, token->buffer);
|
|
|
|
|
|
|
|
if (ret == EFI_SUCCESS && token->event) {
|
|
|
|
token->status = EFI_SUCCESS;
|
|
|
|
efi_signal_event(token->event);
|
|
|
|
}
|
|
|
|
|
2019-09-06 19:37:21 +00:00
|
|
|
out:
|
2017-09-13 22:05:34 +00:00
|
|
|
return EFI_EXIT(ret);
|
|
|
|
}
|
|
|
|
|
2018-10-07 03:26:26 +00:00
|
|
|
/**
|
|
|
|
* efi_file_getpos() - get current position in file
|
|
|
|
*
|
|
|
|
* This function implements the GetPosition service of the EFI file protocol.
|
|
|
|
* See the UEFI spec for details.
|
|
|
|
*
|
|
|
|
* @file: file handle
|
|
|
|
* @pos: pointer to file position
|
|
|
|
* Return: status code
|
|
|
|
*/
|
2017-09-13 22:05:34 +00:00
|
|
|
static efi_status_t EFIAPI efi_file_getpos(struct efi_file_handle *file,
|
2018-10-07 03:26:26 +00:00
|
|
|
u64 *pos)
|
2017-09-13 22:05:34 +00:00
|
|
|
{
|
2018-10-07 03:26:26 +00:00
|
|
|
efi_status_t ret = EFI_SUCCESS;
|
2017-09-13 22:05:34 +00:00
|
|
|
struct file_handle *fh = to_fh(file);
|
2018-04-03 20:37:11 +00:00
|
|
|
|
2017-09-13 22:05:34 +00:00
|
|
|
EFI_ENTRY("%p, %p", file, pos);
|
2018-04-03 20:37:11 +00:00
|
|
|
|
2018-10-07 03:26:26 +00:00
|
|
|
if (fh->isdir) {
|
|
|
|
ret = EFI_UNSUPPORTED;
|
|
|
|
goto out;
|
2018-04-03 20:37:11 +00:00
|
|
|
}
|
2018-10-07 03:26:26 +00:00
|
|
|
|
|
|
|
*pos = fh->offset;
|
|
|
|
out:
|
|
|
|
return EFI_EXIT(ret);
|
2017-09-13 22:05:34 +00:00
|
|
|
}
|
|
|
|
|
2018-10-07 03:26:26 +00:00
|
|
|
/**
|
|
|
|
* efi_file_setpos() - set current position in file
|
|
|
|
*
|
|
|
|
* This function implements the SetPosition service of the EFI file protocol.
|
|
|
|
* See the UEFI spec for details.
|
|
|
|
*
|
|
|
|
* @file: file handle
|
|
|
|
* @pos: new file position
|
|
|
|
* Return: status code
|
|
|
|
*/
|
2017-09-13 22:05:34 +00:00
|
|
|
static efi_status_t EFIAPI efi_file_setpos(struct efi_file_handle *file,
|
2018-10-07 03:26:26 +00:00
|
|
|
u64 pos)
|
2017-09-13 22:05:34 +00:00
|
|
|
{
|
|
|
|
struct file_handle *fh = to_fh(file);
|
|
|
|
efi_status_t ret = EFI_SUCCESS;
|
|
|
|
|
2018-10-07 03:26:26 +00:00
|
|
|
EFI_ENTRY("%p, %llu", file, pos);
|
2017-09-13 22:05:34 +00:00
|
|
|
|
|
|
|
if (fh->isdir) {
|
|
|
|
if (pos != 0) {
|
|
|
|
ret = EFI_UNSUPPORTED;
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
fs_closedir(fh->dirs);
|
|
|
|
fh->dirs = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pos == ~0ULL) {
|
|
|
|
loff_t file_size;
|
|
|
|
|
2019-09-07 21:28:04 +00:00
|
|
|
ret = efi_get_file_size(fh, &file_size);
|
|
|
|
if (ret != EFI_SUCCESS)
|
2017-09-13 22:05:34 +00:00
|
|
|
goto error;
|
|
|
|
pos = file_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
fh->offset = pos;
|
|
|
|
|
|
|
|
error:
|
|
|
|
return EFI_EXIT(ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
static efi_status_t EFIAPI efi_file_getinfo(struct efi_file_handle *file,
|
2018-04-04 13:42:09 +00:00
|
|
|
const efi_guid_t *info_type,
|
2018-04-03 20:37:11 +00:00
|
|
|
efi_uintn_t *buffer_size,
|
|
|
|
void *buffer)
|
2017-09-13 22:05:34 +00:00
|
|
|
{
|
|
|
|
struct file_handle *fh = to_fh(file);
|
|
|
|
efi_status_t ret = EFI_SUCCESS;
|
2019-09-07 19:05:45 +00:00
|
|
|
u16 *dst;
|
2017-09-13 22:05:34 +00:00
|
|
|
|
2018-09-14 20:46:34 +00:00
|
|
|
EFI_ENTRY("%p, %pUl, %p, %p", file, info_type, buffer_size, buffer);
|
2017-09-13 22:05:34 +00:00
|
|
|
|
2019-09-08 08:45:31 +00:00
|
|
|
if (!file || !info_type || !buffer_size ||
|
|
|
|
(*buffer_size && !buffer)) {
|
|
|
|
ret = EFI_INVALID_PARAMETER;
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
2017-09-13 22:05:34 +00:00
|
|
|
if (!guidcmp(info_type, &efi_file_info_guid)) {
|
|
|
|
struct efi_file_info *info = buffer;
|
|
|
|
char *filename = basename(fh);
|
|
|
|
unsigned int required_size;
|
|
|
|
loff_t file_size;
|
|
|
|
|
|
|
|
/* check buffer size: */
|
2019-09-07 19:05:45 +00:00
|
|
|
required_size = sizeof(*info) +
|
|
|
|
2 * (utf8_utf16_strlen(filename) + 1);
|
2017-09-13 22:05:34 +00:00
|
|
|
if (*buffer_size < required_size) {
|
|
|
|
*buffer_size = required_size;
|
|
|
|
ret = EFI_BUFFER_TOO_SMALL;
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
2019-09-07 21:28:04 +00:00
|
|
|
ret = efi_get_file_size(fh, &file_size);
|
|
|
|
if (ret != EFI_SUCCESS)
|
2017-09-13 22:05:34 +00:00
|
|
|
goto error;
|
|
|
|
|
|
|
|
memset(info, 0, required_size);
|
|
|
|
|
|
|
|
info->size = required_size;
|
|
|
|
info->file_size = file_size;
|
|
|
|
info->physical_size = file_size;
|
|
|
|
|
|
|
|
if (fh->isdir)
|
|
|
|
info->attribute |= EFI_FILE_DIRECTORY;
|
|
|
|
|
2019-09-07 19:05:45 +00:00
|
|
|
dst = info->file_name;
|
|
|
|
utf8_utf16_strcpy(&dst, filename);
|
2018-04-04 13:42:11 +00:00
|
|
|
} else if (!guidcmp(info_type, &efi_file_system_info_guid)) {
|
|
|
|
struct efi_file_system_info *info = buffer;
|
2020-05-10 17:39:57 +00:00
|
|
|
struct disk_partition part;
|
2018-04-04 13:42:11 +00:00
|
|
|
efi_uintn_t required_size;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
if (fh->fs->part >= 1)
|
|
|
|
r = part_get_info(fh->fs->desc, fh->fs->part, &part);
|
|
|
|
else
|
|
|
|
r = part_get_info_whole_disk(fh->fs->desc, &part);
|
|
|
|
if (r < 0) {
|
|
|
|
ret = EFI_DEVICE_ERROR;
|
|
|
|
goto error;
|
|
|
|
}
|
2019-09-08 08:32:54 +00:00
|
|
|
required_size = sizeof(*info) + 2;
|
2018-04-04 13:42:11 +00:00
|
|
|
if (*buffer_size < required_size) {
|
|
|
|
*buffer_size = required_size;
|
|
|
|
ret = EFI_BUFFER_TOO_SMALL;
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
memset(info, 0, required_size);
|
|
|
|
|
|
|
|
info->size = required_size;
|
2019-12-08 09:02:37 +00:00
|
|
|
/*
|
|
|
|
* TODO: We cannot determine if the volume can be written to.
|
|
|
|
*/
|
|
|
|
info->read_only = false;
|
2018-04-04 13:42:11 +00:00
|
|
|
info->volume_size = part.size * part.blksz;
|
2019-12-08 09:02:37 +00:00
|
|
|
/*
|
|
|
|
* 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;
|
2018-04-04 13:42:11 +00:00
|
|
|
info->block_size = part.blksz;
|
|
|
|
/*
|
|
|
|
* TODO: The volume label is not available in U-Boot.
|
|
|
|
*/
|
2019-09-08 08:32:54 +00:00
|
|
|
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;
|
2017-09-13 22:05:34 +00:00
|
|
|
} else {
|
|
|
|
ret = EFI_UNSUPPORTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
error:
|
|
|
|
return EFI_EXIT(ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
static efi_status_t EFIAPI efi_file_setinfo(struct efi_file_handle *file,
|
2018-04-04 13:42:09 +00:00
|
|
|
const efi_guid_t *info_type,
|
2018-04-03 20:37:11 +00:00
|
|
|
efi_uintn_t buffer_size,
|
|
|
|
void *buffer)
|
2017-09-13 22:05:34 +00:00
|
|
|
{
|
2019-04-06 16:17:39 +00:00
|
|
|
struct file_handle *fh = to_fh(file);
|
|
|
|
efi_status_t ret = EFI_UNSUPPORTED;
|
|
|
|
|
|
|
|
EFI_ENTRY("%p, %pUl, %zu, %p", file, info_type, buffer_size, buffer);
|
2018-04-03 20:37:11 +00:00
|
|
|
|
2019-04-06 16:17:39 +00:00
|
|
|
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;
|
|
|
|
|
2019-09-08 09:37:07 +00:00
|
|
|
/* The buffer will always contain a file name. */
|
|
|
|
if (buffer_size < sizeof(struct efi_file_info) + 2 ||
|
|
|
|
buffer_size < info->size) {
|
2019-04-06 16:17:39 +00:00
|
|
|
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 */
|
2020-11-10 06:24:16 +00:00
|
|
|
new_file_name = malloc(utf16_utf8_strlen(info->file_name) + 1);
|
2019-04-06 16:17:39 +00:00
|
|
|
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 */
|
2019-09-07 21:28:04 +00:00
|
|
|
ret = efi_get_file_size(fh, &file_size);
|
|
|
|
if (ret != EFI_SUCCESS)
|
2019-04-06 16:17:39 +00:00
|
|
|
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 {
|
2019-09-08 09:37:07 +00:00
|
|
|
/* TODO: We do not support changing the volume label */
|
2019-04-06 16:17:39 +00:00
|
|
|
ret = EFI_UNSUPPORTED;
|
|
|
|
}
|
|
|
|
out:
|
|
|
|
return EFI_EXIT(ret);
|
2017-09-13 22:05:34 +00:00
|
|
|
}
|
|
|
|
|
2021-01-01 07:39:43 +00:00
|
|
|
/**
|
|
|
|
* efi_file_flush_int() - flush file
|
|
|
|
*
|
|
|
|
* This is the internal implementation of the Flush() and FlushEx() services of
|
|
|
|
* the EFI_FILE_PROTOCOL.
|
|
|
|
*
|
|
|
|
* @this: file protocol instance
|
|
|
|
* Return: status code
|
|
|
|
*/
|
|
|
|
static efi_status_t efi_file_flush_int(struct efi_file_handle *this)
|
2017-09-13 22:05:34 +00:00
|
|
|
{
|
2021-01-01 07:39:43 +00:00
|
|
|
struct file_handle *fh = to_fh(this);
|
2017-09-13 22:05:34 +00:00
|
|
|
|
2021-01-01 07:39:43 +00:00
|
|
|
if (!this)
|
|
|
|
return EFI_INVALID_PARAMETER;
|
2019-09-08 07:35:32 +00:00
|
|
|
|
2021-01-01 07:39:43 +00:00
|
|
|
if (!(fh->open_mode & EFI_FILE_MODE_WRITE))
|
|
|
|
return EFI_ACCESS_DENIED;
|
|
|
|
|
|
|
|
/* TODO: flush for file position after end of file */
|
|
|
|
return EFI_SUCCESS;
|
2019-09-08 07:35:32 +00:00
|
|
|
}
|
|
|
|
|
2021-01-01 07:39:43 +00:00
|
|
|
/**
|
|
|
|
* efi_file_flush() - flush file
|
|
|
|
*
|
|
|
|
* This function implements the Flush() service of the EFI_FILE_PROTOCOL.
|
|
|
|
*
|
|
|
|
* See the Unified Extensible Firmware Interface (UEFI) specification for
|
|
|
|
* details.
|
|
|
|
*
|
|
|
|
* @this: file protocol instance
|
|
|
|
* Return: status code
|
|
|
|
*/
|
|
|
|
static efi_status_t EFIAPI efi_file_flush(struct efi_file_handle *this)
|
2019-09-08 07:35:32 +00:00
|
|
|
{
|
2021-01-01 07:39:43 +00:00
|
|
|
efi_status_t ret;
|
|
|
|
|
|
|
|
EFI_ENTRY("%p", this);
|
|
|
|
|
|
|
|
ret = efi_file_flush_int(this);
|
|
|
|
|
|
|
|
return EFI_EXIT(ret);
|
2019-09-08 07:35:32 +00:00
|
|
|
}
|
|
|
|
|
2021-01-01 07:39:43 +00:00
|
|
|
/**
|
|
|
|
* efi_file_flush_ex() - flush file
|
|
|
|
*
|
|
|
|
* This function implements the FlushEx() service of the EFI_FILE_PROTOCOL.
|
|
|
|
*
|
|
|
|
* See the Unified Extensible Firmware Interface (UEFI) specification for
|
|
|
|
* details.
|
|
|
|
*
|
|
|
|
* @this: file protocol instance
|
|
|
|
* @token: transaction token
|
|
|
|
* Return: status code
|
|
|
|
*/
|
|
|
|
static efi_status_t EFIAPI efi_file_flush_ex(struct efi_file_handle *this,
|
|
|
|
struct efi_file_io_token *token)
|
2019-09-08 07:35:32 +00:00
|
|
|
{
|
2021-01-01 07:39:43 +00:00
|
|
|
efi_status_t ret;
|
|
|
|
|
|
|
|
EFI_ENTRY("%p, %p", this, token);
|
|
|
|
|
|
|
|
if (!token) {
|
|
|
|
ret = EFI_INVALID_PARAMETER;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = efi_file_flush_int(this);
|
|
|
|
|
|
|
|
if (ret == EFI_SUCCESS && token->event) {
|
|
|
|
token->status = EFI_SUCCESS;
|
|
|
|
efi_signal_event(token->event);
|
|
|
|
}
|
|
|
|
|
|
|
|
out:
|
|
|
|
return EFI_EXIT(ret);
|
2019-09-08 07:35:32 +00:00
|
|
|
}
|
|
|
|
|
2017-09-13 22:05:34 +00:00
|
|
|
static const struct efi_file_handle efi_file_handle_protocol = {
|
2019-09-08 07:35:32 +00:00
|
|
|
.rev = EFI_FILE_PROTOCOL_REVISION2,
|
2017-09-13 22:05:34 +00:00
|
|
|
.open = efi_file_open,
|
|
|
|
.close = efi_file_close,
|
|
|
|
.delete = efi_file_delete,
|
|
|
|
.read = efi_file_read,
|
|
|
|
.write = efi_file_write,
|
|
|
|
.getpos = efi_file_getpos,
|
|
|
|
.setpos = efi_file_setpos,
|
|
|
|
.getinfo = efi_file_getinfo,
|
|
|
|
.setinfo = efi_file_setinfo,
|
|
|
|
.flush = efi_file_flush,
|
2019-09-08 07:35:32 +00:00
|
|
|
.open_ex = efi_file_open_ex,
|
|
|
|
.read_ex = efi_file_read_ex,
|
|
|
|
.write_ex = efi_file_write_ex,
|
|
|
|
.flush_ex = efi_file_flush_ex,
|
2017-09-13 22:05:34 +00:00
|
|
|
};
|
|
|
|
|
2019-02-04 20:24:35 +00:00
|
|
|
/**
|
|
|
|
* efi_file_from_path() - open file via device path
|
|
|
|
*
|
|
|
|
* @fp: device path
|
|
|
|
* @return: EFI_FILE_PROTOCOL for the file or NULL
|
|
|
|
*/
|
2017-09-13 22:05:34 +00:00
|
|
|
struct efi_file_handle *efi_file_from_path(struct efi_device_path *fp)
|
|
|
|
{
|
|
|
|
struct efi_simple_file_system_protocol *v;
|
|
|
|
struct efi_file_handle *f;
|
|
|
|
efi_status_t ret;
|
|
|
|
|
|
|
|
v = efi_fs_from_path(fp);
|
|
|
|
if (!v)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
EFI_CALL(ret = v->open_volume(v, &f));
|
|
|
|
if (ret != EFI_SUCCESS)
|
|
|
|
return NULL;
|
|
|
|
|
2019-02-04 20:24:35 +00:00
|
|
|
/* Skip over device-path nodes before the file path. */
|
2017-09-13 22:05:34 +00:00
|
|
|
while (fp && !EFI_DP_TYPE(fp, MEDIA_DEVICE, FILE_PATH))
|
|
|
|
fp = efi_dp_next(fp);
|
|
|
|
|
2019-02-04 20:24:35 +00:00
|
|
|
/*
|
|
|
|
* Step through the nodes of the directory path until the actual file
|
|
|
|
* node is reached which is the final node in the device path.
|
|
|
|
*/
|
2017-09-13 22:05:34 +00:00
|
|
|
while (fp) {
|
|
|
|
struct efi_device_path_file_path *fdp =
|
|
|
|
container_of(fp, struct efi_device_path_file_path, dp);
|
|
|
|
struct efi_file_handle *f2;
|
2019-07-14 18:14:46 +00:00
|
|
|
u16 *filename;
|
2017-09-13 22:05:34 +00:00
|
|
|
|
|
|
|
if (!EFI_DP_TYPE(fp, MEDIA_DEVICE, FILE_PATH)) {
|
|
|
|
printf("bad file path!\n");
|
|
|
|
f->close(f);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2019-07-14 18:14:46 +00:00
|
|
|
filename = u16_strdup(fdp->str);
|
|
|
|
if (!filename)
|
|
|
|
return NULL;
|
|
|
|
EFI_CALL(ret = f->open(f, &f2, filename,
|
2017-09-13 22:05:34 +00:00
|
|
|
EFI_FILE_MODE_READ, 0));
|
2019-07-14 18:14:46 +00:00
|
|
|
free(filename);
|
2017-09-13 22:05:34 +00:00
|
|
|
if (ret != EFI_SUCCESS)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
fp = efi_dp_next(fp);
|
|
|
|
|
|
|
|
EFI_CALL(f->close(f));
|
|
|
|
f = f2;
|
|
|
|
}
|
|
|
|
|
|
|
|
return f;
|
|
|
|
}
|
|
|
|
|
|
|
|
static efi_status_t EFIAPI
|
|
|
|
efi_open_volume(struct efi_simple_file_system_protocol *this,
|
|
|
|
struct efi_file_handle **root)
|
|
|
|
{
|
|
|
|
struct file_system *fs = to_fs(this);
|
|
|
|
|
|
|
|
EFI_ENTRY("%p, %p", this, root);
|
|
|
|
|
2018-09-11 06:59:12 +00:00
|
|
|
*root = file_open(fs, NULL, NULL, 0, 0);
|
2017-09-13 22:05:34 +00:00
|
|
|
|
|
|
|
return EFI_EXIT(EFI_SUCCESS);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct efi_simple_file_system_protocol *
|
|
|
|
efi_simple_file_system(struct blk_desc *desc, int part,
|
|
|
|
struct efi_device_path *dp)
|
|
|
|
{
|
|
|
|
struct file_system *fs;
|
|
|
|
|
|
|
|
fs = calloc(1, sizeof(*fs));
|
|
|
|
fs->base.rev = EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_REVISION;
|
|
|
|
fs->base.open_volume = efi_open_volume;
|
|
|
|
fs->desc = desc;
|
|
|
|
fs->part = part;
|
|
|
|
fs->dp = dp;
|
|
|
|
|
|
|
|
return &fs->base;
|
|
|
|
}
|