Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 266 lines (211 sloc) 6.074 kb
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
/*
* GRUB -- GRand Unified Bootloader
* Copyright (C) 2001 Free Software Foundation, Inc.
*
* 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; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#include "shared.h"
#include "filesys.h"

#ifdef PLATFORM_EFI
#undef GRUB_UTIL

#include <grub/types.h>
#include <grub/misc.h>
#include <grub/efi/efi.h>
#include <grub/efi/misc.h>
#include <grub/efi/api.h>

static grub_efi_simple_file_system_t *file_system;
static grub_efi_guid_t fsinfo_guid = GRUB_EFI_DEVICE_PATH_GUID;
grub_efi_file_t *root = NULL;
grub_efi_file_t *file = NULL;

typedef struct {
  grub_efi_uint64_t size;
  grub_efi_uint64_t filesize;
  grub_efi_uint64_t physicalsize;
  grub_efi_time_t createtime;
  grub_efi_time_t accesstime;
  grub_efi_time_t modifytime;
  grub_efi_uint64_t attribute;
  grub_efi_char16_t filename[];
} grub_efi_file_info_t;

int
uefi_mount (void)
{
  grub_efi_handle_t dev_handle = grub_efidisk_get_current_bdev_handle();
  grub_efi_status_t status;

  if (!dev_handle)
    return 0;
  
  file_system = grub_efi_open_protocol (dev_handle, &simple_file_system_guid,
GRUB_EFI_OPEN_PROTOCOL_GET_PROTOCOL);

  if (!file_system)
    return 0;

  status = Call_Service_2 (file_system->open_volume, file_system, &root);

  if (status != GRUB_EFI_SUCCESS)
    return 0;

  return 1;
}

int
uefi_dir (char *dirname)
{
  grub_efi_status_t status;
  grub_efi_char16_t *file_name_w = NULL;
  grub_efi_char16_t *dir_name_w = NULL;
  grub_efi_file_info_t *fileinfo = NULL;
  grub_efi_uintn_t buffersize = 0;
  grub_efi_file_t *directory = NULL;
  int i, dirlen = 0, ret = 0;

  file_name_w = grub_malloc (2 * strlen(dirname) + 2);
  if (!file_name_w)
    goto done;

  for (i=0; i<strlen(dirname); i++) {
    file_name_w[i] = dirname[i];
    if (file_name_w[i] == '/') {
      file_name_w[i] = '\\';
      dirlen = i;
    }
  }

  file_name_w[i] = '\0';

  status = Call_Service_5 (root->open, root, &file, file_name_w,
GRUB_EFI_FILE_MODE_READ, 0);

  if (status != GRUB_EFI_SUCCESS)
    goto done;

  if (dirname[i-1] == '/') {
    if (print_possibilities)
      grub_printf("\n");

    while (1) {
      int filenamelen;

      status = Call_Service_3 (file->read, file, &buffersize, fileinfo);

      if (status == GRUB_EFI_BUFFER_TOO_SMALL) {
fileinfo = grub_malloc(buffersize);
continue;
      } else if (status) {
goto done;
      } else if (buffersize == 0) {
ret = 1;
if (print_possibilities)
grub_printf("\n");
goto done;
      }

      filenamelen = fileinfo->size - sizeof(*fileinfo);

      if (print_possibilities) {
for (i=0; i<filenamelen/2; i++)
grub_printf("%c", (char)fileinfo->filename[i]);
      }
      grub_printf(" ");
    }
  } else {
    char *data = NULL;
    dir_name_w = grub_malloc (2 * dirlen + 2);
    if (!dir_name_w)
      goto done;

    for (i=0; i<dirlen; i++)
      dir_name_w[i] = file_name_w[i];

    dir_name_w[i] = '\0';

    status = Call_Service_5 (root->open, root, &directory, dir_name_w,
GRUB_EFI_FILE_MODE_READ, 0);

    while (1) {
      int filenamelen;
      int invalid = 0;

      status = Call_Service_3 (directory->read, directory, &buffersize, fileinfo);

      if (status == GRUB_EFI_BUFFER_TOO_SMALL) {
fileinfo = grub_malloc(buffersize);
continue;
      } else if (status) {
goto done;
      } else if (buffersize == 0) {
goto done;
      }

      filenamelen = fileinfo->size - sizeof(*fileinfo);

      if (filenamelen != ((strlen(dirname) - dirlen) * 2))
continue;

      for (i=0; i<filenamelen/2; i++) {
if (fileinfo->filename[i] != file_name_w[i + dirlen + 1]) {
if (fileinfo->filename[i] > 0xff ||
(grub_tolower(fileinfo->filename[i]) !=
grub_tolower(file_name_w[i + dirlen + 1])))
invalid = 1;
}
      }

      if (!invalid)
break;
    }

    if (fileinfo->filesize < 256 && fileinfo->filesize > 3)
      {
data = grub_malloc(fileinfo->filesize);
filepos = 0;

uefi_read(data, fileinfo->filesize);

/*
* UEFI doesn't really support symlinks. Apple's UEFI driver exposes
* them as files containing the path of the target. This provides
* hacky support in the absence of a real driver.
*/
if (data[0] == '.' && data[1] == '.' && data[2] == '/')
{
int j;
char *tmpdir = grub_malloc(dirlen + fileinfo->filesize);

for (j=0; j<dirlen+1; j++)
tmpdir[j] = dirname[j];
for (j=0; j<fileinfo->filesize; j++)
tmpdir[j+dirlen+1] = data[j];
tmpdir[j+dirlen+1] = '\0';

/* Open the new file */
ret = uefi_dir(tmpdir);
grub_free (tmpdir);
grub_free (data);
goto done;
}
      }

    if (data)
      grub_free(data);
    ret = 1;
    filemax = fileinfo->filesize;
    filepos = 0;
  }

 done:
  if (fileinfo)
    grub_free (fileinfo);
  if (dir_name_w)
    grub_free (dir_name_w);
  if (file_name_w)
    grub_free (file_name_w);

  return ret;
}

void
uefi_close (void)
{
  grub_efi_status_t status = 0;

  if (file)
    status = Call_Service_1 (file->close, file);

  file = NULL;
}

int
uefi_read (char *addr, int len)
{
  grub_efi_status_t status;
  grub_efi_uintn_t length = len;

  errnum = 0;

  status = Call_Service_2 (file->set_position, file, filepos);

  if (status != GRUB_EFI_SUCCESS)
    goto out;

  status = Call_Service_3 (file->read, file, &length, addr);
  
  len = length;

  if (status == GRUB_EFI_SUCCESS) {
    filepos += len;
    return len;
  }

 out:
  errnum = ERR_FILE_NOT_FOUND;

  return 0;
}
#endif
Something went wrong with that request. Please try again.