~hamo/ubuntu/precise/grub2/grub2.hi_res

« back to all changes in this revision

Viewing changes to grub-core/disk/efi/efidisk.c

  • Committer: Bazaar Package Importer
  • Author(s): Colin Watson, Colin Watson, Robert Millan, Updated translations
  • Date: 2010-11-22 12:24:56 UTC
  • mfrom: (1.26.4 upstream) (17.3.36 sid)
  • mto: (17.3.43 sid)
  • mto: This revision was merged to the branch mainline in revision 89.
  • Revision ID: james.westby@ubuntu.com-20101122122456-y82z3sfb7k4zfdcc
Tags: 1.99~20101122-1
[ Colin Watson ]
* New Bazaar snapshot.  Too many changes to list in full, but some of the
  more user-visible ones are as follows:
  - GRUB script:
    + Function parameters, "break", "continue", "shift", "setparams",
      "return", and "!".
    + "export" command supports multiple variable names.
    + Multi-line quoted strings support.
    + Wildcard expansion.
  - sendkey support.
  - USB hotunplugging and USB serial support.
  - Rename CD-ROM to cd on BIOS.
  - Add new --boot-directory option to grub-install, grub-reboot, and
    grub-set-default; the old --root-directory option is still accepted
    but was often confusing.
  - Basic btrfs detection/UUID support (but no file reading yet).
  - bash-completion for utilities.
  - If a device is listed in device.map, always assume that it is
    BIOS-visible rather than using extra layers such as LVM or RAID.
  - Add grub-mknetdir script (closes: #550658).
  - Remove deprecated "root" command.
  - Handle RAID devices containing virtio components.
  - GRUB Legacy configuration file support (via grub-menulst2cfg).
  - Keyboard layout support (via grub-mklayout and grub-kbdcomp).
  - Check generated grub.cfg for syntax errors before saving.
  - Pause execution for at most ten seconds if any errors are displayed,
    so that the user has a chance to see them.
  - Support submenus.
  - Write embedding zone using Reed-Solomon, so that it's robust against
    being partially overwritten (closes: #550702, #591416, #593347).
  - GRUB_DISABLE_LINUX_RECOVERY and GRUB_DISABLE_NETBSD_RECOVERY merged
    into a single GRUB_DISABLE_RECOVERY variable.
  - Fix loader memory allocation failure (closes: #551627).
  - Don't call savedefault on recovery entries (closes: #589325).
  - Support triple-indirect blocks on ext2 (closes: #543924).
  - Recognise DDF1 fake RAID (closes: #603354).

[ Robert Millan ]
* Use dpkg architecture wildcards.

[ Updated translations ]
* Slovenian (Vanja Cvelbar).  Closes: #604003
* Dzongkha (dawa pemo via Tenzin Dendup).  Closes: #604102

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *  GRUB  --  GRand Unified Bootloader
 
3
 *  Copyright (C) 2006,2007,2008  Free Software Foundation, Inc.
 
4
 *
 
5
 *  GRUB is free software: you can redistribute it and/or modify
 
6
 *  it under the terms of the GNU General Public License as published by
 
7
 *  the Free Software Foundation, either version 3 of the License, or
 
8
 *  (at your option) any later version.
 
9
 *
 
10
 *  GRUB is distributed in the hope that it will be useful,
 
11
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
 *  GNU General Public License for more details.
 
14
 *
 
15
 *  You should have received a copy of the GNU General Public License
 
16
 *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
 
17
 */
 
18
 
 
19
#include <grub/disk.h>
 
20
#include <grub/partition.h>
 
21
#include <grub/mm.h>
 
22
#include <grub/types.h>
 
23
#include <grub/misc.h>
 
24
#include <grub/err.h>
 
25
#include <grub/term.h>
 
26
#include <grub/efi/api.h>
 
27
#include <grub/efi/efi.h>
 
28
#include <grub/efi/disk.h>
 
29
 
 
30
struct grub_efidisk_data
 
31
{
 
32
  grub_efi_handle_t handle;
 
33
  grub_efi_device_path_t *device_path;
 
34
  grub_efi_device_path_t *last_device_path;
 
35
  grub_efi_block_io_t *block_io;
 
36
  grub_efi_disk_io_t *disk_io;
 
37
  struct grub_efidisk_data *next;
 
38
};
 
39
 
 
40
/* GUIDs.  */
 
41
static grub_efi_guid_t disk_io_guid = GRUB_EFI_DISK_IO_GUID;
 
42
static grub_efi_guid_t block_io_guid = GRUB_EFI_BLOCK_IO_GUID;
 
43
 
 
44
static struct grub_efidisk_data *fd_devices;
 
45
static struct grub_efidisk_data *hd_devices;
 
46
static struct grub_efidisk_data *cd_devices;
 
47
 
 
48
/* Duplicate a device path.  */
 
49
static grub_efi_device_path_t *
 
50
duplicate_device_path (const grub_efi_device_path_t *dp)
 
51
{
 
52
  grub_efi_device_path_t *p;
 
53
  grub_size_t total_size = 0;
 
54
 
 
55
  for (p = (grub_efi_device_path_t *) dp;
 
56
       ;
 
57
       p = GRUB_EFI_NEXT_DEVICE_PATH (p))
 
58
    {
 
59
      total_size += GRUB_EFI_DEVICE_PATH_LENGTH (p);
 
60
      if (GRUB_EFI_END_ENTIRE_DEVICE_PATH (p))
 
61
        break;
 
62
    }
 
63
 
 
64
  p = grub_malloc (total_size);
 
65
  if (! p)
 
66
    return 0;
 
67
 
 
68
  grub_memcpy (p, dp, total_size);
 
69
  return p;
 
70
}
 
71
 
 
72
/* Return the device path node right before the end node.  */
 
73
static grub_efi_device_path_t *
 
74
find_last_device_path (const grub_efi_device_path_t *dp)
 
75
{
 
76
  grub_efi_device_path_t *next, *p;
 
77
 
 
78
  if (GRUB_EFI_END_ENTIRE_DEVICE_PATH (dp))
 
79
    return 0;
 
80
 
 
81
  for (p = (grub_efi_device_path_t *) dp, next = GRUB_EFI_NEXT_DEVICE_PATH (p);
 
82
       ! GRUB_EFI_END_ENTIRE_DEVICE_PATH (next);
 
83
       p = next, next = GRUB_EFI_NEXT_DEVICE_PATH (next))
 
84
    ;
 
85
 
 
86
  return p;
 
87
}
 
88
 
 
89
/* Compare device paths.  */
 
90
static int
 
91
compare_device_paths (const grub_efi_device_path_t *dp1,
 
92
                      const grub_efi_device_path_t *dp2)
 
93
{
 
94
  if (! dp1 || ! dp2)
 
95
    /* Return non-zero.  */
 
96
    return 1;
 
97
 
 
98
  while (1)
 
99
    {
 
100
      grub_efi_uint8_t type1, type2;
 
101
      grub_efi_uint8_t subtype1, subtype2;
 
102
      grub_efi_uint16_t len1, len2;
 
103
      int ret;
 
104
 
 
105
      type1 = GRUB_EFI_DEVICE_PATH_TYPE (dp1);
 
106
      type2 = GRUB_EFI_DEVICE_PATH_TYPE (dp2);
 
107
 
 
108
      if (type1 != type2)
 
109
        return (int) type2 - (int) type1;
 
110
 
 
111
      subtype1 = GRUB_EFI_DEVICE_PATH_SUBTYPE (dp1);
 
112
      subtype2 = GRUB_EFI_DEVICE_PATH_SUBTYPE (dp2);
 
113
 
 
114
      if (subtype1 != subtype2)
 
115
        return (int) subtype1 - (int) subtype2;
 
116
 
 
117
      len1 = GRUB_EFI_DEVICE_PATH_LENGTH (dp1);
 
118
      len2 = GRUB_EFI_DEVICE_PATH_LENGTH (dp2);
 
119
 
 
120
      if (len1 != len2)
 
121
        return (int) len1 - (int) len2;
 
122
 
 
123
      ret = grub_memcmp (dp1, dp2, len1);
 
124
      if (ret != 0)
 
125
        return ret;
 
126
 
 
127
      if (GRUB_EFI_END_ENTIRE_DEVICE_PATH (dp1))
 
128
        break;
 
129
 
 
130
      dp1 = (grub_efi_device_path_t *) ((char *) dp1 + len1);
 
131
      dp2 = (grub_efi_device_path_t *) ((char *) dp2 + len2);
 
132
    }
 
133
 
 
134
  return 0;
 
135
}
 
136
 
 
137
static struct grub_efidisk_data *
 
138
make_devices (void)
 
139
{
 
140
  grub_efi_uintn_t num_handles;
 
141
  grub_efi_handle_t *handles;
 
142
  grub_efi_handle_t *handle;
 
143
  struct grub_efidisk_data *devices = 0;
 
144
 
 
145
  /* Find handles which support the disk io interface.  */
 
146
  handles = grub_efi_locate_handle (GRUB_EFI_BY_PROTOCOL, &disk_io_guid,
 
147
                                    0, &num_handles);
 
148
  if (! handles)
 
149
    return 0;
 
150
 
 
151
  /* Make a linked list of devices.  */
 
152
  for (handle = handles; num_handles--; handle++)
 
153
    {
 
154
      grub_efi_device_path_t *dp;
 
155
      grub_efi_device_path_t *ldp;
 
156
      struct grub_efidisk_data *d;
 
157
      grub_efi_block_io_t *bio;
 
158
      grub_efi_disk_io_t *dio;
 
159
 
 
160
      dp = grub_efi_get_device_path (*handle);
 
161
      if (! dp)
 
162
        continue;
 
163
 
 
164
      ldp = find_last_device_path (dp);
 
165
      if (! ldp)
 
166
        /* This is empty. Why?  */
 
167
        continue;
 
168
 
 
169
      bio = grub_efi_open_protocol (*handle, &block_io_guid,
 
170
                                    GRUB_EFI_OPEN_PROTOCOL_GET_PROTOCOL);
 
171
      dio = grub_efi_open_protocol (*handle, &disk_io_guid,
 
172
                                    GRUB_EFI_OPEN_PROTOCOL_GET_PROTOCOL);
 
173
      if (! bio || ! dio)
 
174
        /* This should not happen... Why?  */
 
175
        continue;
 
176
 
 
177
      d = grub_malloc (sizeof (*d));
 
178
      if (! d)
 
179
        {
 
180
          /* Uggh.  */
 
181
          grub_free (handles);
 
182
          return 0;
 
183
        }
 
184
 
 
185
      d->handle = *handle;
 
186
      d->device_path = dp;
 
187
      d->last_device_path = ldp;
 
188
      d->block_io = bio;
 
189
      d->disk_io = dio;
 
190
      d->next = devices;
 
191
      devices = d;
 
192
    }
 
193
 
 
194
  grub_free (handles);
 
195
 
 
196
  return devices;
 
197
}
 
198
 
 
199
/* Find the parent device.  */
 
200
static struct grub_efidisk_data *
 
201
find_parent_device (struct grub_efidisk_data *devices,
 
202
                    struct grub_efidisk_data *d)
 
203
{
 
204
  grub_efi_device_path_t *dp, *ldp;
 
205
  struct grub_efidisk_data *parent;
 
206
 
 
207
  dp = duplicate_device_path (d->device_path);
 
208
  if (! dp)
 
209
    return 0;
 
210
 
 
211
  ldp = find_last_device_path (dp);
 
212
  ldp->type = GRUB_EFI_END_DEVICE_PATH_TYPE;
 
213
  ldp->subtype = GRUB_EFI_END_ENTIRE_DEVICE_PATH_SUBTYPE;
 
214
  ldp->length[0] = sizeof (*ldp);
 
215
  ldp->length[1] = 0;
 
216
 
 
217
  for (parent = devices; parent; parent = parent->next)
 
218
    {
 
219
      /* Ignore itself.  */
 
220
      if (parent == d)
 
221
        continue;
 
222
 
 
223
      if (compare_device_paths (parent->device_path, dp) == 0)
 
224
        {
 
225
          /* Found.  */
 
226
          if (! parent->last_device_path)
 
227
            parent = 0;
 
228
 
 
229
          break;
 
230
        }
 
231
    }
 
232
 
 
233
  grub_free (dp);
 
234
  return parent;
 
235
}
 
236
 
 
237
static int
 
238
iterate_child_devices (struct grub_efidisk_data *devices,
 
239
                       struct grub_efidisk_data *d,
 
240
                       int (*hook) (struct grub_efidisk_data *child))
 
241
{
 
242
  struct grub_efidisk_data *p;
 
243
 
 
244
  for (p = devices; p; p = p->next)
 
245
    {
 
246
      grub_efi_device_path_t *dp, *ldp;
 
247
 
 
248
      dp = duplicate_device_path (p->device_path);
 
249
      if (! dp)
 
250
        return 0;
 
251
 
 
252
      ldp = find_last_device_path (dp);
 
253
      ldp->type = GRUB_EFI_END_DEVICE_PATH_TYPE;
 
254
      ldp->subtype = GRUB_EFI_END_ENTIRE_DEVICE_PATH_SUBTYPE;
 
255
      ldp->length[0] = sizeof (*ldp);
 
256
      ldp->length[1] = 0;
 
257
 
 
258
      if (compare_device_paths (dp, d->device_path) == 0)
 
259
        if (hook (p))
 
260
          {
 
261
            grub_free (dp);
 
262
            return 1;
 
263
          }
 
264
 
 
265
      grub_free (dp);
 
266
    }
 
267
 
 
268
  return 0;
 
269
}
 
270
 
 
271
/* Add a device into a list of devices in an ascending order.  */
 
272
static void
 
273
add_device (struct grub_efidisk_data **devices, struct grub_efidisk_data *d)
 
274
{
 
275
  struct grub_efidisk_data **p;
 
276
  struct grub_efidisk_data *n;
 
277
 
 
278
  for (p = devices; *p; p = &((*p)->next))
 
279
    {
 
280
      int ret;
 
281
 
 
282
      ret = compare_device_paths (find_last_device_path ((*p)->device_path),
 
283
                                  find_last_device_path (d->device_path));
 
284
      if (ret == 0)
 
285
        ret = compare_device_paths ((*p)->device_path,
 
286
                                    d->device_path);
 
287
      if (ret == 0)
 
288
        return;
 
289
      else if (ret > 0)
 
290
        break;
 
291
    }
 
292
 
 
293
  n = grub_malloc (sizeof (*n));
 
294
  if (! n)
 
295
    return;
 
296
 
 
297
  grub_memcpy (n, d, sizeof (*n));
 
298
  n->next = (*p);
 
299
  (*p) = n;
 
300
}
 
301
 
 
302
/* Name the devices.  */
 
303
static void
 
304
name_devices (struct grub_efidisk_data *devices)
 
305
{
 
306
  struct grub_efidisk_data *d;
 
307
 
 
308
  /* First, identify devices by media device paths.  */
 
309
  for (d = devices; d; d = d->next)
 
310
    {
 
311
      grub_efi_device_path_t *dp;
 
312
 
 
313
      dp = d->last_device_path;
 
314
      if (! dp)
 
315
        continue;
 
316
 
 
317
      if (GRUB_EFI_DEVICE_PATH_TYPE (dp) == GRUB_EFI_MEDIA_DEVICE_PATH_TYPE)
 
318
        {
 
319
          int is_hard_drive = 0;
 
320
 
 
321
          switch (GRUB_EFI_DEVICE_PATH_SUBTYPE (dp))
 
322
            {
 
323
            case GRUB_EFI_HARD_DRIVE_DEVICE_PATH_SUBTYPE:
 
324
              is_hard_drive = 1;
 
325
              /* Fall through by intention.  */
 
326
            case GRUB_EFI_CDROM_DEVICE_PATH_SUBTYPE:
 
327
              {
 
328
                struct grub_efidisk_data *parent;
 
329
 
 
330
                parent = find_parent_device (devices, d);
 
331
                if (parent)
 
332
                  {
 
333
                    if (is_hard_drive)
 
334
                      {
 
335
#if 0
 
336
                        grub_printf ("adding a hard drive by a partition: ");
 
337
                        grub_print_device_path (parent->device_path);
 
338
#endif
 
339
                        add_device (&hd_devices, parent);
 
340
                      }
 
341
                    else
 
342
                      {
 
343
#if 0
 
344
                        grub_printf ("adding a cdrom by a partition: ");
 
345
                        grub_print_device_path (parent->device_path);
 
346
#endif
 
347
                        add_device (&cd_devices, parent);
 
348
                      }
 
349
 
 
350
                    /* Mark the parent as used.  */
 
351
                    parent->last_device_path = 0;
 
352
                  }
 
353
              }
 
354
              /* Mark itself as used.  */
 
355
              d->last_device_path = 0;
 
356
              break;
 
357
 
 
358
            default:
 
359
              /* For now, ignore the others.  */
 
360
              break;
 
361
            }
 
362
        }
 
363
    }
 
364
 
 
365
  /* Let's see what can be added more.  */
 
366
  for (d = devices; d; d = d->next)
 
367
    {
 
368
      grub_efi_device_path_t *dp;
 
369
      grub_efi_block_io_media_t *m;
 
370
 
 
371
      dp = d->last_device_path;
 
372
      if (! dp)
 
373
        continue;
 
374
 
 
375
      m = d->block_io->media;
 
376
      if (m->logical_partition)
 
377
        {
 
378
          /* Only one partition in a non-media device. Assume that this
 
379
             is a floppy drive.  */
 
380
#if 0
 
381
          grub_printf ("adding a floppy by guessing: ");
 
382
          grub_print_device_path (d->device_path);
 
383
#endif
 
384
          add_device (&fd_devices, d);
 
385
        }
 
386
      else if (m->read_only && m->block_size > GRUB_DISK_SECTOR_SIZE)
 
387
        {
 
388
          /* This check is too heuristic, but assume that this is a
 
389
             CDROM drive.  */
 
390
#if 0
 
391
          grub_printf ("adding a cdrom by guessing: ");
 
392
          grub_print_device_path (d->device_path);
 
393
#endif
 
394
          add_device (&cd_devices, d);
 
395
        }
 
396
      else
 
397
        {
 
398
          /* The default is a hard drive.  */
 
399
#if 0
 
400
          grub_printf ("adding a hard drive by guessing: ");
 
401
          grub_print_device_path (d->device_path);
 
402
#endif
 
403
          add_device (&hd_devices, d);
 
404
        }
 
405
    }
 
406
}
 
407
 
 
408
static void
 
409
free_devices (struct grub_efidisk_data *devices)
 
410
{
 
411
  struct grub_efidisk_data *p, *q;
 
412
 
 
413
  for (p = devices; p; p = q)
 
414
    {
 
415
      q = p->next;
 
416
      grub_free (p);
 
417
    }
 
418
}
 
419
 
 
420
/* Enumerate all disks to name devices.  */
 
421
static void
 
422
enumerate_disks (void)
 
423
{
 
424
  struct grub_efidisk_data *devices;
 
425
 
 
426
  devices = make_devices ();
 
427
  if (! devices)
 
428
    return;
 
429
 
 
430
  name_devices (devices);
 
431
  free_devices (devices);
 
432
}
 
433
 
 
434
static int
 
435
grub_efidisk_iterate (int (*hook) (const char *name))
 
436
{
 
437
  struct grub_efidisk_data *d;
 
438
  char buf[16];
 
439
  int count;
 
440
 
 
441
  for (d = fd_devices, count = 0; d; d = d->next, count++)
 
442
    {
 
443
      grub_snprintf (buf, sizeof (buf), "fd%d", count);
 
444
      grub_dprintf ("efidisk", "iterating %s\n", buf);
 
445
      if (hook (buf))
 
446
        return 1;
 
447
    }
 
448
 
 
449
  for (d = hd_devices, count = 0; d; d = d->next, count++)
 
450
    {
 
451
      grub_snprintf (buf, sizeof (buf), "hd%d", count);
 
452
      grub_dprintf ("efidisk", "iterating %s\n", buf);
 
453
      if (hook (buf))
 
454
        return 1;
 
455
    }
 
456
 
 
457
  for (d = cd_devices, count = 0; d; d = d->next, count++)
 
458
    {
 
459
      grub_snprintf (buf, sizeof (buf), "cd%d", count);
 
460
      grub_dprintf ("efidisk", "iterating %s\n", buf);
 
461
      if (hook (buf))
 
462
        return 1;
 
463
    }
 
464
 
 
465
  return 0;
 
466
}
 
467
 
 
468
static int
 
469
get_drive_number (const char *name)
 
470
{
 
471
  unsigned long drive;
 
472
 
 
473
  if ((name[0] != 'f' && name[0] != 'h' && name[0] != 'c') || name[1] != 'd')
 
474
    goto fail;
 
475
 
 
476
  drive = grub_strtoul (name + 2, 0, 10);
 
477
  if (grub_errno != GRUB_ERR_NONE)
 
478
    goto fail;
 
479
 
 
480
  return (int) drive ;
 
481
 
 
482
 fail:
 
483
  grub_error (GRUB_ERR_UNKNOWN_DEVICE, "not a efidisk");
 
484
  return -1;
 
485
}
 
486
 
 
487
static struct grub_efidisk_data *
 
488
get_device (struct grub_efidisk_data *devices, int num)
 
489
{
 
490
  struct grub_efidisk_data *d;
 
491
 
 
492
  for (d = devices; d && num; d = d->next, num--)
 
493
    ;
 
494
 
 
495
  if (num == 0)
 
496
    return d;
 
497
 
 
498
  return 0;
 
499
}
 
500
 
 
501
static grub_err_t
 
502
grub_efidisk_open (const char *name, struct grub_disk *disk)
 
503
{
 
504
  int num;
 
505
  struct grub_efidisk_data *d = 0;
 
506
  grub_efi_block_io_media_t *m;
 
507
 
 
508
  grub_dprintf ("efidisk", "opening %s\n", name);
 
509
 
 
510
  num = get_drive_number (name);
 
511
  if (num < 0)
 
512
    return grub_errno;
 
513
 
 
514
  switch (name[0])
 
515
    {
 
516
    case 'f':
 
517
      d = get_device (fd_devices, num);
 
518
      break;
 
519
    case 'c':
 
520
      d = get_device (cd_devices, num);
 
521
      break;
 
522
    case 'h':
 
523
      d = get_device (hd_devices, num);
 
524
      break;
 
525
    default:
 
526
      /* Never reach here.  */
 
527
      break;
 
528
    }
 
529
 
 
530
  if (! d)
 
531
    return grub_error (GRUB_ERR_UNKNOWN_DEVICE, "no such device");
 
532
 
 
533
  disk->id = ((num << 8) | name[0]);
 
534
  m = d->block_io->media;
 
535
  /* FIXME: Probably it is better to store the block size in the disk,
 
536
     and total sectors should be replaced with total blocks.  */
 
537
  grub_dprintf ("efidisk", "m = %p, last block = %llx, block size = %x\n",
 
538
                m, (unsigned long long) m->last_block, m->block_size);
 
539
  disk->total_sectors = (m->last_block
 
540
                         * (m->block_size >> GRUB_DISK_SECTOR_BITS));
 
541
  disk->data = d;
 
542
 
 
543
  grub_dprintf ("efidisk", "opening %s succeeded\n", name);
 
544
 
 
545
  return GRUB_ERR_NONE;
 
546
}
 
547
 
 
548
static void
 
549
grub_efidisk_close (struct grub_disk *disk __attribute__ ((unused)))
 
550
{
 
551
  /* EFI disks do not allocate extra memory, so nothing to do here.  */
 
552
  grub_dprintf ("efidisk", "closing %s\n", disk->name);
 
553
}
 
554
 
 
555
static grub_err_t
 
556
grub_efidisk_read (struct grub_disk *disk, grub_disk_addr_t sector,
 
557
                   grub_size_t size, char *buf)
 
558
{
 
559
  /* For now, use the disk io interface rather than the block io's.  */
 
560
  struct grub_efidisk_data *d;
 
561
  grub_efi_disk_io_t *dio;
 
562
  grub_efi_block_io_t *bio;
 
563
  grub_efi_status_t status;
 
564
 
 
565
  d = disk->data;
 
566
  dio = d->disk_io;
 
567
  bio = d->block_io;
 
568
 
 
569
  grub_dprintf ("efidisk",
 
570
                "reading 0x%lx sectors at the sector 0x%llx from %s\n",
 
571
                (unsigned long) size, (unsigned long long) sector, disk->name);
 
572
 
 
573
  status = efi_call_5 (dio->read, dio, bio->media->media_id,
 
574
                      (grub_efi_uint64_t) sector << GRUB_DISK_SECTOR_BITS,
 
575
                      (grub_efi_uintn_t) size << GRUB_DISK_SECTOR_BITS,
 
576
                      buf);
 
577
  if (status != GRUB_EFI_SUCCESS)
 
578
    return grub_error (GRUB_ERR_READ_ERROR, "efidisk read error");
 
579
 
 
580
  return GRUB_ERR_NONE;
 
581
}
 
582
 
 
583
static grub_err_t
 
584
grub_efidisk_write (struct grub_disk *disk, grub_disk_addr_t sector,
 
585
                    grub_size_t size, const char *buf)
 
586
{
 
587
  /* For now, use the disk io interface rather than the block io's.  */
 
588
  struct grub_efidisk_data *d;
 
589
  grub_efi_disk_io_t *dio;
 
590
  grub_efi_block_io_t *bio;
 
591
  grub_efi_status_t status;
 
592
 
 
593
  d = disk->data;
 
594
  dio = d->disk_io;
 
595
  bio = d->block_io;
 
596
 
 
597
  grub_dprintf ("efidisk",
 
598
                "writing 0x%lx sectors at the sector 0x%llx to %s\n",
 
599
                (unsigned long) size, (unsigned long long) sector, disk->name);
 
600
 
 
601
  status = efi_call_5 (dio->write, dio, bio->media->media_id,
 
602
                       (grub_efi_uint64_t) sector << GRUB_DISK_SECTOR_BITS,
 
603
                       (grub_efi_uintn_t) size << GRUB_DISK_SECTOR_BITS,
 
604
                       (void *) buf);
 
605
  if (status != GRUB_EFI_SUCCESS)
 
606
    return grub_error (GRUB_ERR_WRITE_ERROR, "efidisk write error");
 
607
 
 
608
  return GRUB_ERR_NONE;
 
609
}
 
610
 
 
611
static struct grub_disk_dev grub_efidisk_dev =
 
612
  {
 
613
    .name = "efidisk",
 
614
    .id = GRUB_DISK_DEVICE_EFIDISK_ID,
 
615
    .iterate = grub_efidisk_iterate,
 
616
    .open = grub_efidisk_open,
 
617
    .close = grub_efidisk_close,
 
618
    .read = grub_efidisk_read,
 
619
    .write = grub_efidisk_write,
 
620
    .next = 0
 
621
  };
 
622
 
 
623
void
 
624
grub_efidisk_init (void)
 
625
{
 
626
  enumerate_disks ();
 
627
  grub_disk_dev_register (&grub_efidisk_dev);
 
628
}
 
629
 
 
630
void
 
631
grub_efidisk_fini (void)
 
632
{
 
633
  free_devices (fd_devices);
 
634
  free_devices (hd_devices);
 
635
  free_devices (cd_devices);
 
636
  grub_disk_dev_unregister (&grub_efidisk_dev);
 
637
}
 
638
 
 
639
/* Some utility functions to map GRUB devices with EFI devices.  */
 
640
grub_efi_handle_t
 
641
grub_efidisk_get_device_handle (grub_disk_t disk)
 
642
{
 
643
  struct grub_efidisk_data *d;
 
644
  char type;
 
645
 
 
646
  if (disk->dev->id != GRUB_DISK_DEVICE_EFIDISK_ID)
 
647
    return 0;
 
648
 
 
649
  d = disk->data;
 
650
  type = disk->name[0];
 
651
 
 
652
  switch (type)
 
653
    {
 
654
    case 'f':
 
655
      /* This is the simplest case.  */
 
656
      return d->handle;
 
657
 
 
658
    case 'c':
 
659
      /* FIXME: probably this is not correct.  */
 
660
      return d->handle;
 
661
 
 
662
    case 'h':
 
663
      /* If this is the whole disk, just return its own data.  */
 
664
      if (! disk->partition)
 
665
        return d->handle;
 
666
 
 
667
      /* Otherwise, we must query the corresponding device to the firmware.  */
 
668
      {
 
669
        struct grub_efidisk_data *devices;
 
670
        grub_efi_handle_t handle = 0;
 
671
        auto int find_partition (struct grub_efidisk_data *c);
 
672
 
 
673
        int find_partition (struct grub_efidisk_data *c)
 
674
          {
 
675
            grub_efi_hard_drive_device_path_t hd;
 
676
 
 
677
            grub_memcpy (&hd, c->last_device_path, sizeof (hd));
 
678
 
 
679
            if ((GRUB_EFI_DEVICE_PATH_TYPE (c->last_device_path)
 
680
                 == GRUB_EFI_MEDIA_DEVICE_PATH_TYPE)
 
681
                && (GRUB_EFI_DEVICE_PATH_SUBTYPE (c->last_device_path)
 
682
                    == GRUB_EFI_HARD_DRIVE_DEVICE_PATH_SUBTYPE)
 
683
                && (grub_partition_get_start (disk->partition)
 
684
                    == hd.partition_start)
 
685
                && (grub_partition_get_len (disk->partition)
 
686
                    == hd.partition_size))
 
687
              {
 
688
                handle = c->handle;
 
689
                return 1;
 
690
              }
 
691
 
 
692
            return 0;
 
693
          }
 
694
 
 
695
        devices = make_devices ();
 
696
        iterate_child_devices (devices, d, find_partition);
 
697
        free_devices (devices);
 
698
 
 
699
        if (handle != 0)
 
700
          return handle;
 
701
      }
 
702
      break;
 
703
 
 
704
    default:
 
705
      break;
 
706
    }
 
707
 
 
708
  return 0;
 
709
}
 
710
 
 
711
char *
 
712
grub_efidisk_get_device_name (grub_efi_handle_t *handle)
 
713
{
 
714
  grub_efi_device_path_t *dp, *ldp;
 
715
 
 
716
  dp = grub_efi_get_device_path (handle);
 
717
  if (! dp)
 
718
    return 0;
 
719
 
 
720
  ldp = find_last_device_path (dp);
 
721
  if (! ldp)
 
722
    return 0;
 
723
 
 
724
  if (GRUB_EFI_DEVICE_PATH_TYPE (ldp) == GRUB_EFI_MEDIA_DEVICE_PATH_TYPE
 
725
      && (GRUB_EFI_DEVICE_PATH_SUBTYPE (ldp)
 
726
          == GRUB_EFI_HARD_DRIVE_DEVICE_PATH_SUBTYPE))
 
727
    {
 
728
      /* This is a hard disk partition.  */
 
729
      grub_disk_t parent = 0;
 
730
      grub_partition_t tpart = NULL;
 
731
      char *device_name;
 
732
      grub_efi_device_path_t *dup_dp, *dup_ldp;
 
733
      grub_efi_hard_drive_device_path_t hd;
 
734
      auto int find_parent_disk (const char *name);
 
735
      auto int find_partition (grub_disk_t disk, const grub_partition_t part);
 
736
 
 
737
      /* Find the disk which is the parent of a given hard disk partition.  */
 
738
      int find_parent_disk (const char *name)
 
739
        {
 
740
          grub_disk_t disk;
 
741
 
 
742
          disk = grub_disk_open (name);
 
743
          if (! disk)
 
744
            return 1;
 
745
 
 
746
          if (disk->dev->id == GRUB_DISK_DEVICE_EFIDISK_ID)
 
747
            {
 
748
              struct grub_efidisk_data *d;
 
749
 
 
750
              d = disk->data;
 
751
              if (compare_device_paths (d->device_path, dup_dp) == 0)
 
752
                {
 
753
                  parent = disk;
 
754
                  return 1;
 
755
                }
 
756
            }
 
757
 
 
758
          grub_disk_close (disk);
 
759
          return 0;
 
760
        }
 
761
 
 
762
      /* Find the identical partition.  */
 
763
      int find_partition (grub_disk_t disk __attribute__ ((unused)),
 
764
                          const grub_partition_t part)
 
765
        {
 
766
          if (grub_partition_get_start (part) == hd.partition_start
 
767
              && grub_partition_get_len (part) == hd.partition_size)
 
768
            {
 
769
              tpart = part;
 
770
              return 1;
 
771
            }
 
772
 
 
773
          return 0;
 
774
        }
 
775
 
 
776
      /* It is necessary to duplicate the device path so that GRUB
 
777
         can overwrite it.  */
 
778
      dup_dp = duplicate_device_path (dp);
 
779
      if (! dup_dp)
 
780
        return 0;
 
781
 
 
782
      dup_ldp = find_last_device_path (dup_dp);
 
783
      dup_ldp->type = GRUB_EFI_END_DEVICE_PATH_TYPE;
 
784
      dup_ldp->subtype = GRUB_EFI_END_ENTIRE_DEVICE_PATH_SUBTYPE;
 
785
      dup_ldp->length[0] = sizeof (*dup_ldp);
 
786
      dup_ldp->length[1] = 0;
 
787
 
 
788
      grub_efidisk_iterate (find_parent_disk);
 
789
      grub_free (dup_dp);
 
790
 
 
791
      if (! parent)
 
792
        return 0;
 
793
 
 
794
      /* Find a partition which matches the hard drive device path.  */
 
795
      grub_memcpy (&hd, ldp, sizeof (hd));
 
796
      grub_partition_iterate (parent, find_partition);
 
797
 
 
798
      if (! tpart)
 
799
        {
 
800
          grub_disk_close (parent);
 
801
          return 0;
 
802
        }
 
803
 
 
804
      {
 
805
        char *partition_name = grub_partition_get_name (tpart);
 
806
        device_name = grub_xasprintf ("%s,%s", parent->name, partition_name);
 
807
        grub_free (partition_name);
 
808
      }
 
809
      grub_disk_close (parent);
 
810
 
 
811
      return device_name;
 
812
    }
 
813
  else
 
814
    {
 
815
      /* This should be an entire disk.  */
 
816
      auto int find_disk (const char *name);
 
817
      char *device_name = 0;
 
818
 
 
819
      int find_disk (const char *name)
 
820
        {
 
821
          grub_disk_t disk;
 
822
 
 
823
          disk = grub_disk_open (name);
 
824
          if (! disk)
 
825
            return 1;
 
826
 
 
827
          if (disk->dev->id == GRUB_DISK_DEVICE_EFIDISK_ID)
 
828
            {
 
829
              struct grub_efidisk_data *d;
 
830
 
 
831
              d = disk->data;
 
832
              if (compare_device_paths (d->device_path, dp) == 0)
 
833
                {
 
834
                  device_name = grub_strdup (disk->name);
 
835
                  grub_disk_close (disk);
 
836
                  return 1;
 
837
                }
 
838
            }
 
839
 
 
840
          grub_disk_close (disk);
 
841
          return 0;
 
842
 
 
843
        }
 
844
 
 
845
      grub_efidisk_iterate (find_disk);
 
846
      return device_name;
 
847
    }
 
848
 
 
849
  return 0;
 
850
}