1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
  
     | 
    
      /* ----------------------------------------------------------------------- *
 *
 *   Copyright 2007-2008 H. Peter Anvin - All Rights Reserved
 *   Copyright 2012 Intel Corporation; author: H. Peter Anvin
 *   Chandramouli Narayanan
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, Inc., 53 Temple Place Ste 330,
 *   Boston MA 02111-1307, USA; either version 2 of the License, or
 *   (at your option) any later version; incorporated herein by reference.
 *
 * ----------------------------------------------------------------------- */
/* Miscellaneous functions for UEFI support
 * We assume that EFI library initialization has completed
 * and we have access to the global EFI exported variables
 *
 */
#include "efi.h"
#include "fio.h"
/* Variables that need to be exported
 * efi_errno - maintains the errors from EFI calls to display error messages.
 */
EFI_STATUS efi_errno = EFI_SUCCESS;
/* Locals
 * vol_root - handle to the root device for file operations
 */
static EFI_FILE_HANDLE vol_root;
/* Table of UEFI error messages to be indexed with the EFI errno
 * Update error message list as needed
 */
static CHAR16 *uefi_errmsg[] = {
	L"EFI_UNDEFINED",	/* should not get here */
	L"EFI_LOAD_ERROR",
	L"EFI_INVALID_PARAMETER",
	L"EFI_UNSUPPORTED",
	L"EFI_BAD_BUFFER_SIZE",
	L"EFI_BUFFER_TOO_SMALL",
	L"EFI_NOT_READY",
	L"EFI_DEVICE_ERROR",
	L"EFI_WRITE_PROTECTED",
	L"EFI_OUT_OF_RESOURCES",
	L"EFI_VOLUME_CORRUPTED",
	L"EFI_VOLUME_FULL",
	L"EFI_NO_MEDIA",
	L"EFI_MEDIA_CHANGED",
	L"EFI_NOT_FOUND",
	L"EFI_ACCESS_DENIED",
	L"EFI_NO_RESPONSE",
	L"EFI_NO_MAPPING",
	L"EFI_TIMEOUT",
	L"EFI_NOT_STARTED",
	L"EFI_ALREADY_STARTED",
	L"EFI_ABORTED",
	L"EFI_ICMP_ERROR",
	L"EFI_TFTP_ERROR",
	L"EFI_PROTOCOL_ERROR"
};
static UINTN nerrs = sizeof(uefi_errmsg)/sizeof(CHAR16 *);
/* Generic write error message; there is no gnu lib api to write to StdErr
 * For now, everything goes ConOut
 */
void efi_printerr(
    CHAR16   *fmt,
    ...
    )
{
    va_list     args;
    va_start (args, fmt);
    VPrint (fmt, args);
    va_end (args);
}
/* Simple console logger of efi-specific error messages. It uses
 * gnu-efi library Print function to do the job.
 */
void efi_perror(CHAR16 *prog)
{
	/* Ensure that the err number lies within range
	 * Beware: unsigned comparisons fail on efi, signed comparisons work
	 */
	if (EFI_ERROR(efi_errno) && (INTN)efi_errno < (INTN)nerrs)
		efi_printerr(L"%s: %s\n", prog, uefi_errmsg[efi_errno]);
}
/* Write to UEFI ConOut */
void efi_printout(
    CHAR16   *fmt,
    ...
    )
{
    va_list     args;
    va_start (args, fmt);
    VPrint (fmt, args);
    va_end (args);
}
/* IMPORTANT:
 * efi_setvol_root() needs to be called from efi main.
 * The rest of the ADV support relies on the file i/o environment
 * setup here. In order to use the EFI file support, we need
 * to set up the volume root. Subsequent file operations need the root to
 * access the interface routines.
 *
 */
EFI_STATUS efi_set_volroot(EFI_HANDLE device_handle)
{
	vol_root = LibOpenRoot(device_handle);
	if (!vol_root) {
		return EFI_DEVICE_ERROR;
	}
	return EFI_SUCCESS;
}
/* File operations using EFI runtime services */
/* Open the file using EFI runtime service
 * Opening a file in EFI requires a handle to the device
 * root in order to use the interface to the file operations supported by UEFI.
 * For now, assume device volume root handle from the loaded image
 *
 * Return a valid handle if open succeeded and null otherwise.
 * UEFI returns a bogus handle on error, so return null handle on error.
 *
 * TODO:
 * 1. Validate the assumption about the root device
 * 2. Can EFI open a file with full path name specification?
 * 3. Look into gnu-efi helper functions for dealing with device path/file path
 * 4. Consider utilizing EFI file open attributes.
 * 5. In EFI, file attributes can be specified only at the time of creation.
 * How do we support the equivalent of set_attributes() and clear_attributes()
 */
EFI_FILE_HANDLE efi_open(CHAR16 *file, UINT64 mode)
{
	/* initialize with NULL handle since EFI open returns bogus */
	EFI_FILE_HANDLE	fd = NULL;
	ASSERT(vol_root);
	/* Note that the attributes parameter is none for now */
	efi_errno = uefi_call_wrapper(vol_root->Open,
					5,
					vol_root,
					&fd,
					file,
					mode,
					0);
	return fd;
}
/*
 * read/write wrapper functions for UEFI
 *
 * Read or write the specified number of bytes starting at the
 * offset specified.
 *
 * Returns:
 * number of bytes read/written on success
 * -1 on error
 */
/* Wrapper function to read from a file */
size_t efi_xpread(EFI_FILE_HANDLE fd, void *buf, size_t count, off_t offset)
{
	ASSERT(fd);
	efi_errno = uefi_call_wrapper(fd->SetPosition,
					2,
				    fd,
				    offset);
	if (EFI_ERROR(efi_errno)) return -1;
	efi_errno = uefi_call_wrapper(fd->Read,
					3,
				    fd,
				    &count,
					buf);
	if (EFI_ERROR(efi_errno)) return -1;
	return count;
}
/* Wrapper function to write */
size_t efi_xpwrite(EFI_FILE_HANDLE fd, void *buf, size_t count, off_t offset)
{
	ASSERT(fd);
	efi_errno = uefi_call_wrapper(fd->SetPosition,
					2,
				    fd,
				    offset);
	if (EFI_ERROR(efi_errno)) return -1;
	efi_errno = uefi_call_wrapper(fd->Write,
					3,
				    fd,
				    &count,
					buf);
	if (EFI_ERROR(efi_errno)) return -1;
	return count;
}
/* For an open handle, return the generic file info excluding
 * the variable-length filename in the EFI_FILE_INFO structure.
 */
int efi_fstat(EFI_FILE_HANDLE fd, EFI_FILE_INFO *st)
{
	EFI_FILE_INFO *finfo;
	ASSERT(fd);
	finfo = LibFileInfo(fd);
	if (finfo) {
		uefi_call_wrapper(BS->CopyMem, 3, (VOID *)st, (VOID *)finfo, SIZE_OF_EFI_FILE_INFO);
		FreePool(finfo);
		return 0;
	}
	/* gnu-efi lib does not return EFI status; export a generic device error for now */
	efi_errno = EFI_DEVICE_ERROR;
	return -1;
}
/* set/clear_attributes()
 * 	Currently handles only VFAT filesystem
 * TODO:
 *    1. Assumes VFAT file system.
 *    2. How do we support other file systems?
 */
void efi_set_attributes(EFI_FILE_HANDLE fd)
{
	EFI_FILE_INFO *finfo;
	ASSERT(fd);
	finfo = LibFileInfo(fd);
	if (finfo) {
		/* Hidden+System+Readonly */
		finfo->Attribute = EFI_FILE_READ_ONLY|EFI_FILE_HIDDEN|EFI_FILE_SYSTEM;
		efi_errno = uefi_call_wrapper(fd->SetInfo,
					4,
					fd,
					&GenericFileInfo,
					finfo->Size,
					finfo);
		FreePool(finfo);
	} else efi_errno = EFI_NOT_FOUND;
}
void efi_clear_attributes(EFI_FILE_HANDLE fd)
{
	EFI_FILE_INFO *finfo;
	ASSERT(fd);
	finfo = LibFileInfo(fd);
	if (finfo) {
		finfo->Attribute = 0; /* no attributes */
		efi_errno = uefi_call_wrapper(fd->SetInfo, 
					4, 
					fd,
					&GenericFileInfo,
					finfo->Size,
					finfo);
		FreePool(finfo);
	} else efi_errno = EFI_NOT_FOUND;
}
/* Implement the sync operation using the EFI Flush file operation*/
void efi_sync(EFI_FILE_HANDLE fd)
{
	ASSERT(fd);
	efi_errno = uefi_call_wrapper(fd->Flush, 1, fd);
	return;
}
/* Close the file */
void efi_close(EFI_FILE_HANDLE fd)
{
	ASSERT(fd);
	efi_errno = uefi_call_wrapper(fd->Close, 1, fd);
	return;
}
 
     |